Whenever you concatenate a string, at each concatenation you create a new copy of the string, and both strings are copied, one character at a time. This leads to the time complexity of O (n 2 ) (McDowell).
If you want to improve performance, use
StringBuilder
One of its constructors has the following syntax:
public StringBuilder(int size);
StringBuilder (a mutable sequence of characters. Remember that strings are immmutable) helps solve this problem by simply creating a mutable array from all strings. copying them back to the string only if necessary (McDowell).
StringBuilder str = new StringBuilder(0); str.append(someStr); str.append(3); str.append("]");
Link:
McDowell, Gail Laakmann. Cracking The Coding Interview, 6th Edition. Print.
"Stringbuilder (Java SE 8 platform)." Docs.oracle.com. Np, 2016. Web. June 4, 2016.
fonji
source share