One Two Three
One Two Three

Reputation: 23497

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?

Upvotes: 4

Views: 156

Answers (1)

superEb
superEb

Reputation: 5673

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.

Upvotes: 24

Related Questions