java.lang.UnsupportedOperationException in addAll method
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   saar_noam
Posted On:   Tuesday, August 31, 2004 03:12 AM

I'm using addAll method in a recursive function and sometimes it throws java.lang.UnsupportedOperationException although all values seems o.k the code: public List getChildItemsRecursive() throws Exception { RUMItem rumItems[] = getChildItems(); if(rumItems == null) return new ArrayList(); List list = Arrays.asList(rumItems); for(int i = 0; childGroups != null && i < childGroups.length; i++) { RUMGroup childGroup = childGroups[i]; list.addAll(childGroup.getChildItemsRecursive()); } return list; } tx in advance Saar    More>>

I'm using addAll method in a recursive function
and sometimes it throws java.lang.UnsupportedOperationException
although all values seems o.k
the code:





public List getChildItemsRecursive() throws Exception {

RUMItem rumItems[] = getChildItems();

if(rumItems == null)

return new ArrayList();

List list = Arrays.asList(rumItems);

for(int i = 0; childGroups != null && i < childGroups.length; i++) {

RUMGroup childGroup = childGroups[i];

list.addAll(childGroup.getChildItemsRecursive());

}


return list;

}



tx in advance
Saar

   <<Less

Re: java.lang.UnsupportedOperationException in addAll method

Posted By:   shay_gabay  
Posted On:   Tuesday, September 14, 2004 04:26 AM

The implementation of Arrays do not create a java.util.ArrayList as a result of the method java.util.Arrays.asList(Object[] obj), Instead the result is kind of a readonly list.


The API says "Returns a fixed-size list backed by the specified array...", obj parameter "the array by which the list will be backed.".


meaning that the returned list is a readonly list.


Use a java.util.ArrayList(Collection c) by using:


List newList = new java.util.ArrayList(Arrays.asList(myList));


Best Regards

Gabay Shay

About | Sitemap | Contact