Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create array of StringBuilder initialized with ""?

Tags:

java

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("");
}
like image 581
TheCrazyProgrammer Avatar asked Mar 20 '13 10:03

TheCrazyProgrammer


2 Answers

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.

like image 92
Craig Brett Avatar answered Oct 08 '22 05:10

Craig Brett


It will always be null. You have to initialize it manually if you want "" in there.

Instead you could access the array by a method which returns "" if the value is null.

like image 28
Kai Avatar answered Oct 08 '22 05:10

Kai