Android / Java Append String + int

I have a question, what is the best way to add INT and Lines to create a new line? In the debug highlighting tool, I see too many distributions if I use the + operator.

But I also tried StringBuffer and there are still too many distributions.

Can anybody help me?

thanks

+4
source share
2 answers

Use StringBuilder or StringBuffer , but with enough initial ability to avoid re-allocation. The default capacity is 16, so as soon as you exceed it, the data should be copied to a new location. Use append not + .

 int integer = 5; StringBuilder s = new StringBuilder(100); s.append("something"); s.append(integer); s.append("more text"); 

100 slots will be allocated.

Link: http://developer.android.com/reference/java/lang/StringBuilder.html

+12
source

You indicated that you need to create a "new line", if you create more objects than this, you should be able to optimize them.

Note: it may be possible to delete the entire selection, but only by passing a StringBuilder or something similar throughout the life of the string you created. This may or may not be simple depending on how you use it.

0
source

Source: https://habr.com/ru/post/1311925/


All Articles