File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Does Arrays.asList return a true list? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Does Arrays.asList return a true list?" Watch "Does Arrays.asList return a true list?" New topic
Author

Does Arrays.asList return a true list?

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
In the JavaDocs, it says Arrays.asList returns a List. But in my code when I call the add() method off what it returned, it says it is an unsupported method. And in my debugger when I look at the variable it says that it is of type "Arrays$ArrayList"
Does anyone know which is correct? Is it an array, a List or someting else?
Jehan
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
JehanNYNJ
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
As for your question, can you post a snippet of the code in question so we can see how you are using it and how it being accessed....


Dave
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It's a List. But not all Lists support add() methods - they may throw an exception instead. The documentation for Arrays.asList() tells you what you can and can't do with this particular List (Arrays.ArrayList, which is different from a regualr ArrayList). If you want a List with the same contents but which supports add(), copy the contents into a new ArrayList or LinkedList, like this:
List list = new ArrayList(Arrays.asList(array));
Note that changes to this new list will not affect the original array. (Unlike changes to the Arrays.asList() version, which do affect it.)


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does Arrays.asList return a true list?
 
Similar Threads
Doubt in K&B SCJP 5: topic Converting Arrays to Lists to Arrays
ClassCastException ArrayList->List
Infinte iteration in a list
UnsupportedOperationException
new ArrayList<Integer>({1,2,3,4})