I wrote a Java program, in which, I need to append a string
" u13a2" to an existing one "u1234 u12de u1386 ... u15a3".
So gradually the string becomes longer and longer. I found the time spent on each appending also becomes longer and longer. Is there any way that we can improve this to some extend ?
The implementation came to my mind includes:
unicodeArray += " "+unicode;
or
unicodeArray = unicodeArray.concat(" "+unicode);
They gave similar performance. I think the main reason that causes these bad performance is the special type String. It creates a new object for every assignment. If you also think so, does this mean I'd better use another type, like byte array?
解决方案
Use StringBuilder class. It is more efficient at what you are trying to do.