Have a separate copy of the mStack array with a string representation, initialized to an empty String by default, so your loop will look like this:
String [] mStackCopy = new String[]{"","","","","","","","","","",}; // or mstackCopy = new String[mStack.length]; // for( int i = 0 ; i < mStackCopy.lenght ; i++ ) { mStack[i] = "" }
Also create a StringBuilder with enough capacity:
StringBuilder sb = new StringBuilder( 10000 );// 10k chars or whatever makes sense.
So, when you need to create a message, you simply:
for (int i = 0; i <= step; i++) { sb.append( mStackCopy[i] ); }
And the empty parts will not cause a problem, because they are already empty:
You can even write it hard:
sb.append( mStackCopy[0]); sb.append( mStackCopy[1]); sb.append( mStackCopy[2]); sb.append( mStackCopy[3]); sb.append( mStackCopy[4]); sb.append( mStackCopy[5]); sb.append( mStackCopy[6]); sb.append( mStackCopy[7]); sb.append( mStackCopy[8]); sb.append( mStackCopy[9]);
But it will bring more pain than the relief in the future guaranteed.
When you add something to your mStack:
MStack item = new MStack(); item.setCurrentMessage("Some message"); ....
Just create a copy of the message and add "," already.
addToMStack(int position, MStackItem item ) { mStack[position] = item; mStackCopy[position] = item.getCurrentMessage() + ", "; }
And depending on the appearance of zeros (if its low) you can catch them
addToMStack(int position, MStackItem item ) { if( item == null ) { return; } mStack[position] = item; try { mStackCopy[position] = item.getCurrentMessage() + ", "; } catch( NullPointerException npe ){} }
What is terrifying
Or confirm it:
addToMStack(int position, MStackItem item ) { if( item == null ) { return; } mStack[position] = item; mStackCopy[position] = item.getCurrentMessage() + ", "; }
I'm sure your method does something else that you are not showing us. Probably the reason is here.
In addition, 16% is not so bad if 100% is 1 second.