How to create a StringBuilder array initialized with ""?

How to create an array by calling args contructor?

StringBuilder[] sb=new StringBuilder[100]; 

But if I check sb [0], it is zero. I want sb [0] with sb [99] to be initialized with "".

Error Result:

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

EDIT: Or should I do this:

 for(StringBuilder it:sb) { it=new StringBuilder(""); } 
+7
source share
4 answers

All your code will initialize an array ready for 100 StringBuilders. In fact, it will not be filled.

You can do it:

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

That should do it for you.

+10
source

It will always be null . You must initialize it manually if you want "" there.

Instead, you can access the array using a method that returns "" if null .

+2
source
 StringBuilder[] sb = new StringBuilder[100]; for(int i=0;i<100;i++) { sb[i] = new StringBuilder(""); } 
+1
source

All values ​​in any null array, if not set ... you need to initialize each value manually

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

or...

 StringBuilder[] sb=new StringBuilder[]{new StringBuilder(), new StringBuilder(), etc} 

(I would recommend the first method for an array with lots of entries)

+1
source

All Articles