Have a separate copy of the mStack
array with the string representation, by default initialized with empty String, so your loop would be:
String [] mStackCopy = new String[]{"","","","","","","","","","",};
// or mstackCopy = new String[mStack.length];
// for( int i = 0 ; i < mStackCopy.lenght ; i++ ) { mStack[i] = "" }
Also, create the StringBuilder with enough capacity:
StringBuilder sb = new StringBuilder( 10000 );// 10k chars or whatever makes sense.
So, when you need to create the message you would simply:
for (int i = 0; i <= step; i++) {
sb.append( mStackCopy[i] );
}
And empty parts won't cause a problem because they are blank already:
You may even hard code it:
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 this would cause more pain than relief in the future, guaranteed.
When you add something to your mStack:
MStack item = new MStack();
item.setCurrentMessage("Some message");
....
Just make a copy of the message and append the ", " already.
addToMStack(int position, MStackItem item ) {
mStack[position] = item;
mStackCopy[position] = item.getCurrentMessage() + ", ";
}
And depending on the occurrence of nulls ( 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 ){}
}
Which is horrendous
Or validate it:
addToMStack(int position, MStackItem item ) {
if( item == null ) { return; }
mStack[position] = item;
mStackCopy[position] = item.getCurrentMessage() + ", ";
}
I'm pretty sure your method is doing something else that you don't show us. Probably the reason is there.
Also, 16% is not that bad, if 100% is 1sec.