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,
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,