aspose 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
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: 19092
    
  40

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
 
Consider Paul's rocket mass heater.
 
subject: Problem adding ArrayList to List