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

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?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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....

Jim Yingst

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:
subject: Does Arrays.asList return a true list?
It's not a secret anymore!