wood burning stoves*
The moose likes Java in General and the fly likes Problem adding ArrayList to List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem adding ArrayList to List" Watch "Problem adding ArrayList to List" New topic

Problem adding ArrayList to List

Tom Henricksen
Ranch Hand

Joined: Mar 23, 2004
Posts: 136

This method is throwing an java.lang.UnsupportedOperationException
when I unit test it. It comes from the line with the list.add(inList);

Any suggestions?
[ September 20, 2005: Message edited by: Tom Henricksen ]
Henry Wong

Joined: Sep 28, 2004
Posts: 18520

The Arrays.asList() does not return a fully functional list... Quoted from the Java Docs...

Returns a fixed-size list backed by the specified array.

Since the list that is return is backed by the specified array, it can't change in size. This means add and remove operations do not work.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
One possible way to fix this is with the following:

This will create a new List that you CAN modify. It also adds all the elements from the List that is created by the asList() method.



Java API Documentation
The Java Tutorial
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem adding ArrayList to List
Similar Threads
sublist of arraylist???
Populate Table Rows With Data from AJAX
How to search a name in a group of list
Collection that keeps order
sum of non numeric elements in an ArrayList