Working with String[] and ArrayList

W

Wendy S

I have a String[] (property of a Struts form bean) that I need to manipulate
in Java code.

I would really prefer to work with an ArrayList to avoid the inevitable out
of bounds errors and having to grow the array, etc.

I thought I had it with Arrays.asList( myArray) but that turns out to not
allow items to be added, it just provides a List backed by a fixed size
array. :( Is there a way to turn a String[] into an ArrayList, without
looping?

What's the easiest way to add one to the size of a String[] and avoid all
the usual pitfalls?

I have this:
String[] accounts = new String { "one", "two", "three" };
String[] newArray = new String[ accounts.length + 1];
System.arraycopy( accounts, 0, newArray, 0, accounts.length );
newArray[ newArray.length - 1 ] = "four";
accounts = null;

Comments? Suggestions? Thanks!
 
J

Jos A. Horsmeier

Wendy S said:
I have a String[] (property of a Struts form bean) that I need to manipulate
in Java code.

I would really prefer to work with an ArrayList to avoid the inevitable out
of bounds errors and having to grow the array, etc.

I thought I had it with Arrays.asList( myArray) but that turns out to not
allow items to be added, it just provides a List backed by a fixed size
array. :( Is there a way to turn a String[] into an ArrayList, without
looping?

You're halfway there; after you've created that non-modifiable list,
create *another* ArrayList and you're done --

List temp= Arrays.asList(myArray);
List done= new ArrayList();

done.addAll(temp); // here's your modifiable ArrayList

kind regards,

Jos
 
V

visionset

I thought I had it with Arrays.asList( myArray) but that turns out to not
allow items to be added, it just provides a List backed by a fixed size
array. :( Is there a way to turn a String[] into an ArrayList, without
looping?

new ArrayList(Arrays.asList(myArray))
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Wendy said:
I have a String[] (property of a Struts form bean) that I need to manipulate
in Java code.

I would really prefer to work with an ArrayList to avoid the inevitable out
of bounds errors and having to grow the array, etc.

I thought I had it with Arrays.asList( myArray) but that turns out to not
allow items to be added, it just provides a List backed by a fixed size
array. :( Is there a way to turn a String[] into an ArrayList, without
looping?

What's the easiest way to add one to the size of a String[] and avoid all
the usual pitfalls?

I have this:
String[] accounts = new String { "one", "two", "three" };
String[] newArray = new String[ accounts.length + 1];
System.arraycopy( accounts, 0, newArray, 0, accounts.length );
newArray[ newArray.length - 1 ] = "four";
accounts = null;

This is how ArrayList/StringBuffer etc. work internally. However, avoid
growing the array by only one slot. If you add many items to the array
this will get very expensive very quickly. A general rule of thumb would
be to double the size of the array every time it is full.

It is a lot of work of dubious value to implement something like this
instead of just copying the values into an ArrayList, because sooner or
later you'll need more and more methods from ArrayList, and then
you might as well just use ArrayList in the first place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top