Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:11:07+00:00 2026-05-23T05:11:07+00:00

I’m writing real-time game in Android and I’d like to do not allocate any

  • 0

I’m writing real-time game in Android and I’d like to do not allocate any object in main loop. The problem is String value for score.

I have class, that implements CharSequence and uses StringBuilder:

public class MyString implements CharSequence {

    protected String prefix;
    protected StringBuilder stringBuilder;

    public MyString(String prefix) {
        this.prefix = prefix;
        stringBuilder = new StringBuilder(prefix);
    }

    public void setValue(int value) {
        removeValue();
        stringBuilder.append(value);
    }

    public void setValue(float value) {
        removeValue();
        stringBuilder.append(value);
    }

    private void removeValue() {
        if (prefix == null) {
            stringBuilder.setLength(0);
        } else {
            stringBuilder.setLength(prefix.length());
        }
    }

    @Override
    public char charAt(int index) {
        return stringBuilder.charAt(index);
    }

    @Override
    public int length() {
        return stringBuilder.length();
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return null; // not necessary
    }

}

The usage of this class is:

Paint paint = new Paint();
MyString myString = new MyString("Your score: ");

// Main loop
while(true) {
    myString.setValue(Game.score);
    canvas.drawText(myString, 0, myString.length(), 0, 0, paint);
}

The problem is, that StringBuilder.append(int) creates String object (by String.valueOf(int)) every iteration.

What is your way to solve a problem like this?

My next thought was to convert int and float to array of chars and the setValue() methods would add this array into StringBuilder. The array would be stored in MyString class and it would be created only once and then would be reused. But I can’t find way, how to convert float correctly into array of chars. The int is not problem.

Thank you for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:11:07+00:00Added an answer on May 23, 2026 at 5:11 am

    Here is some C# code that I wrote for exactly this purpose. It should be pretty simple to convert this to Java. The minDigits parameter causes zero-padding on the left — You can skip that if you like.

    // drawInteger doesn't allocate like string.Format()
    int[] digitBuff = new int[10];
    string[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    
    void drawInteger(SpriteFont font, int i, int minDigits, Vector2 pos, Color colour) {
        Debug.Assert(i >= 0 && minDigits > 0 && minDigits <= digitBuff.Length);
        int index = 0;
        while (i > 0 || index < minDigits) {
            digitBuff[index] = i % 10;
            i /= 10;
            ++index;
        }
        while (index > 0) {
            --index;
            string digit = digits[digitBuff[index]];
            sb.DrawString(font, digit, pos, colour);
            pos.X += font.MeasureString(digit).X;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to count the length of a string with PHP. The string
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.