java - How to remove the brackets [ ] from ArrayList#toString()? -


i have created array list in java looks this:

 public static arraylist<integer> error = new arraylist<>();  (int x= 1; x<10; x++)  {      errors.add(x);  } 

when print errors errors

[1,2,3,4,5,6,7,8,9]

now want remove brackets([ ]) array list. thought use method errors.remove("["), discovered boolean , displays true or false. suggest how can achieve this?

thank in advance help.

you calling system.out.println print list. javadoc says:

this method calls @ first string.valueof(x) printed object's string value 

the brackets added tostring implementation of arraylist. remove them, have first string:

string errordisplay = errors.tostring(); 

and strip brackets, this:

errordisplay = errordisplay.substring(1, errordisplay.length() - 1); 

it not practice depend on tostring() implementation. tostring() intended generate human readable representation logging or debugging purposes. better build string whilst iterating:

list<integer> errors = new arraylist<>(); stringbuilder sb = new stringbuilder(); (int x = 1; x<10; x++) {      errors.add(x);     sb.append(x).append(","); } sb.setlength(sb.length() - 1); string errordisplay = sb.tostring(); 

note not array, string displaying contents of list. create array list can use list.toarray():

// create new array same size list integer[] errorsarray = new integer[errors.size()]; // fill array errors.toarray(errorsarray ); 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -