How to create array of StringBuilder initialized with “”?

How to create an array by calling args contructor?

StringBuilder[] sb=new StringBuilder[100];

But if I check sb[0] it is null. I want that sb[0] to sb[99] initialized with “”.

Following results in an error:

StringBuilder[] sb=new StringBuilder[100]("");

EDIT: Or I have to do this:

for(StringBuilder it:sb)
{
  it=new StringBuilder("");
}

Answer

All your code will do is initialise an array ready for 100 StringBuilders. It won’t actually populate it.

You could do this:

StringBuilder[] sb=new StringBuilder[100];

for (int i = 0; i < sb.length; i++) {
    sb[i] = new StringBuilder("");
}

That should do it for you.

Leave a Reply

Your email address will not be published. Required fields are marked *