ArrayList problems

W

Wendy S

I'm expecting ArrayList.remove to, well, remove an item, instead it seems
like it's leaving a null in the list. Here's the problem code:

DynaActionForm dForm = (DynaActionForm) form;
int position = Integer.parseInt( dForm.getString( "position" ) );
String[] accounts = (String[]) dForm.get( "accounts" );
ArrayList tempList = new ArrayList( Arrays.asList( accounts ) );
tempList.remove( position );
tempList.trimToSize();
dForm.set( "accounts", tempList.toArray( accounts ) );
accounts = null;

From the logs, I see:
14:17:01,140 - removeAccount: tempList=[thing, otherThing]
14:17:01,140 - removeAccount: removing # 0
....
14:17:02,140 - removeAccount: tempList=[otherThing, null]
14:17:02,140 - removeAccount: removing # 0

And so when I display the contents of the 'accounts' property, I get blanks
where there shouldn't be anything. Neither remove nor trimToSize appears to
be doing what I want, at least not in conjunction with the String[] behind
it.

The intent of the code is to avoid dealing directly with the String[] which
is a property of a Struts DynaActionForm. So I turn it into an ArrayList,
and then back into a String[]. But I need to *really* remove the item, not
just put a null in its place.

Thanks in advance for any ideas,
 
F

Filip Larsen

Wendy S wrote
I'm expecting ArrayList.remove to, well, remove an item, instead it seems
like it's leaving a null in the list. Here's the problem code:

DynaActionForm dForm = (DynaActionForm) form;
int position = Integer.parseInt( dForm.getString( "position" ) );
String[] accounts = (String[]) dForm.get( "accounts" );
ArrayList tempList = new ArrayList( Arrays.asList( accounts ) );
tempList.remove( position );
tempList.trimToSize();
dForm.set( "accounts", tempList.toArray( accounts ) );
accounts = null;

The ArrayList.toArray(Object[] array) method will copy the content of
the ArrayList into the array if there is room enough. Since there is
always room enough in your case it will be reused with the last element
being set to null. You can use

tempList.toArray(new String[tempList.size()])

instead to force a new array of correct size. And while you are at it, I
recommend leavíng out the call to trimToSize; not only is it
unnecessary, it also waste space and time since you are copying the
whole thing again anyway. In fact, by making the array yourself and not
using ArrayList at all, you can additionally save the space for an array
and the time for 1-2 copies. For instance something like

...
String[] accounts = (String[]) dForm.get( "accounts" );
dForm.set( "accounts", remove(accounts,position) );
...

/** Return new array with one element removed.
* If pos is invalid, the same array is returned.
*/
public static String[] remove(String[] a, int pos) {
if (pos < 0 || pos >= a.length) return a;
String[] b = new String[a.length-1];
System.arrayCopy(a,0,b,0,pos);
if (pos < b.length) {
System.arrayCopy(a,pos,b,pos,b.length-pos);
}
return b;
}


Regards,
 
W

Wendy S

Filip Larsen said:
The ArrayList.toArray(Object[] array) method will copy the content of
the ArrayList into the array if there is room enough. Since there is
always room enough in your case it will be reused with the last element
being set to null. You can use
tempList.toArray(new String[tempList.size()])
instead to force a new array of correct size.

Thanks, that explains the nulls. I ended up just working with the array
directly. I thought ArrayList would be easier and might avoid the out of
bounds errors I'd inevitably create. :)
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top