Search code examples
javaandroidlibgdxstringbuilder

LibGDX Android Stringbuilder most efficient Way?


Good Day to you all, i have a little Question, what would be the best way to often add a lot of different Strings to an other String especially in LibGDX?

blockString=new StringBuilder(blockString).append(String.valueOf(blockCount.length/2+i)).append("][").append(String.valueOf(f)).append("]").append(String.valueOf(t)).toString();

I have to do this method several hundred Times in as little Time as possible. Does anyone have an Idea what would be the most efficient way to do that in LibGdx on Android?

Thanks in advance for the Answers

EDIT

whole loop:

                for (int i=-300;i<300;i++){
                    //whole world Generation(taking up nearly no time)
                    int f=0;
                    int max=50;
                    while (f<max){
                        if (blockCount[blockCount.length/2+i][0][0]==32&&blockCount[blockCount.length/2+i][0][1]==0){
                            max=f;
                        }
                        if (!(blockCount[blockCount.length/2+i][f][0]==32&&blockCount[blockCount.length/2+i][f][1]==0)){
                            int t=codeTexture(blockCount[blockCount.length/2+i][f][0],blockCount[blockCount.length/2+i][f][1]);
                            blockString=blockString.concat("["+String.valueOf(blockCount.length/2+i)+"]"+"["+String.valueOf(f)+"]"+String.valueOf(t));

                        }
                        if ((blockCount[blockCount.length/2+i][f][0]==48&&blockCount[blockCount.length/2+i][f][1]==64)||(blockCount[blockCount.length/2+i][f][0]==32&&blockCount[blockCount.length/2+i][f][1]==64)||(blockCount[blockCount.length/2+i][f][0]==0&&blockCount[blockCount.length/2+i][f][1]==64)||(blockCount[blockCount.length/2+i][f][0]==0&&blockCount[blockCount.length/2+i][f][1]==0)||(blockCount[blockCount.length/2+i][f][0]==16&&blockCount[blockCount.length/2+i][f][1]==16)){
                            max=f+10;
                        }
                        f=f+1;
                    }
                 }

Solution

  • The code you've written will perform exactly the same as the much more straightforward

    blockString += (blockCount.length / 2 + i) + "][" + t;
    

    Edit: to do this in a loop, your code should really look like

    StringBuilder blockStringBuilder = new StringBuilder();
    for (int i=-300;i<300;i++){
                    //whole world Generation(taking up nearly no time)
                    int f=0;
                    int max=50;
                    while (f<max){
                        if (blockCount[blockCount.length/2+i][0][0]==32&&blockCount[blockCount.length/2+i][0][1]==0){
                            max=f;
                        }
                        if (!(blockCount[blockCount.length/2+i][f][0]==32&&blockCount[blockCount.length/2+i][f][1]==0)){
                            int t=codeTexture(blockCount[blockCount.length/2+i][f][0],blockCount[blockCount.length/2+i][f][1]);
                            blockStringBuilder.append("[").append(blockCount.length/2+i).append("][").append(f).append("]").append(t);
    
                        }
                        if ((blockCount[blockCount.length/2+i][f][0]==48&&blockCount[blockCount.length/2+i][f][1]==64)||(blockCount[blockCount.length/2+i][f][0]==32&&blockCount[blockCount.length/2+i][f][1]==64)||(blockCount[blockCount.length/2+i][f][0]==0&&blockCount[blockCount.length/2+i][f][1]==64)||(blockCount[blockCount.length/2+i][f][0]==0&&blockCount[blockCount.length/2+i][f][1]==0)||(blockCount[blockCount.length/2+i][f][0]==16&&blockCount[blockCount.length/2+i][f][1]==16)){
                            max=f+10;
                        }
                        f=f+1;
                    }
                 }
                 blockString = blockStringBuilder.toString();