You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by ja...@wendysmoak.com on 2005/02/01 00:18:38 UTC

Re: [BeanUtils] add item to a list

Marc DeXeT wrote:
> I would use a BeanUtils or PropertyUtils method to add my item.
> Set property doesn't work because of it set item at position, and if
> position greater than list size, you get this old good
> IndexOutOfBoundsException.

I think the writer of the bean class is responsible for making sure the
property has enough room to hold the new value.

I recently learned that although BeanUtils considers a List to be an
indexed property, the JavaBeans specification only deals with Object[]
type properties.  With an array, you would _definitely_ have to check the
size before saying
    array[pos] = obj;

The same thing is true of a List backed property, but we get used to
having the details hidden by the 'add' method.  So, I think your other
'set' method would have to look like this:

set( int pos, Object obj) {
  if ( pos >= list.size() ) {
       //increase size of list
   }
   list.set(pos,obj);
}

Like you, though, I just use getThings/setThings(List) and
addThing(Object). I can't say I've ever had a need to set an individual
item by position.  I wanted to recently when trying to populate a bean
with Digester, but I elected to create the List and set it that way rather
than one by one.

-- 
Wendy Smoak


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org