File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem adding ArrayList to List" Watch "Problem adding ArrayList to List" New topic
Author

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?
Thanks,
Tom
[ September 20, 2005: Message edited by: Tom Henricksen ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

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.

Henry


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.

HTH

Layne


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
Collection that keeps order
How to search a name in a group of list
sum of non numeric elements in an ArrayList
sublist of arraylist???
Populate Table Rows With Data from AJAX