Supposing I have an ArrayList < String > named arr and a StringBuilder named buf, I use the following method to add the contents of buf to arr.
if ( !buf.toString( ).equals( "" ) ) { //allow only non-empty Strings.
arr.add( buf.toString( ) );
}
But it calls buf.toString( ) twice. I don't want that in my code. Moreover, is there any other (short or easy or efficient or something else) way to achieve this?
EDIT: Also, I do this in a loop, so is this efficient?
String s = buf.toString();and uses?