Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringBuilder output does not include character supplied in the constructor

        StringBuilder bd = new StringBuilder('[');
        Iterator<String> iter = names.iterator();
        while (iter.hasNext()) {
            bd.append(iter.next());
            if (iter.hasNext()) {
                bd.append(", ");
            }
        }
        bd.append(']');
        return bd.toString();

I thought the output would come out looking something like [<some stuff, if any>], but it looks like <some stuff, if any>] instead.

What is going on?

like image 288
One Two Three Avatar asked Nov 29 '22 07:11

One Two Three


1 Answers

Change the char in the constructor args to a String.

StringBuilder bd = new StringBuilder("[");

Otherwise the char is being converted to an int to define initial capacity.

like image 79
superEb Avatar answered Dec 04 '22 14:12

superEb