A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
Problem adding ArrayList to List
Tom Henricksen
Ranch Hand
Joined: Mar 23, 2004
Posts: 135
posted
Sep 20, 2005 20:13:00
0
This method is throwing an
java.lang.UnsupportedOperationException
when I
unit
test
it. It comes from the line with the list.add(inList);
public long createRecord(String[] data) throws DuplicateKeyException { List list = Arrays.asList(this.data); ArrayList inList = new ArrayList(); for (int i = 0; i < data.length; i++) { inList.add(data[i]); } inList.add((new String(new Integer(recNo).toString()))); list.add(inList); // THis is the problem this.data = (String[][]) list.toArray(new String[list.size()][]); int createdRecNo = recNo; recNo++; return createdRecNo; }
Any suggestions?
Thanks,
Tom
[ September 20, 2005: Message edited by: Tom Henricksen ]
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
19
I like...
posted
Sep 20, 2005 20:19:00
0
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
posted
Sep 20, 2005 20:34:00
0
One possible way to fix this is with the following:
List list = new ArrayList(Arrays.asList(this.data));
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
sum of non numeric elements in an ArrayList
How to search a name in a group of list
Populate Table Rows With Data from AJAX
sublist of arraylist???
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter