File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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: 20531

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:
subject: Problem adding ArrayList to List
It's not a secret anymore!