Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does Arrays.asList return a true list?

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic