File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using asList with Generics

 
Jerret Halter
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to preform the following:



But the following exception is thrown when I try to run it:

Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at learning.about.collection.Fubar.main(Fubar.java:15)

I only have Java 1.5 on my machine, so what is going wrong here?

Thanks in advance :-)
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the following exception is thrown when I try to run it:

Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at learning.about.collection.Fubar.main(Fubar.java:15)

I only have Java 1.5 on my machine, so what is going wrong here?

Thanks in advance :-)


The Arrays.asList() method returns a list that is *backed* by the array. Since it is not possible to resize an array, you are not allowed to add or removed from the list.

Henry
 
Jerret Halter
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what you are saying is that I can use Arrays.asList() it will build a List, however I can only add to the Array?
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jerret Halter:
So what you are saying is that I can use Arrays.asList() it will build a List, however I can only add to the Array?


The list that you get back from the asList() method can only be used like an array. You can set() a value at an index. You can get() a value at an index. But you can't add an element or remove an element, because you can't grow or shrink an array.

Henry
 
Jerret Halter
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ICIC thanks a bunch.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

what you could do instead in a situation like this is to make a new list out of the array, the array would copied into the list, and you can add things to it.
So instead of saying
String[] myArray = {"d", "c", "z", "q"};
List<String> foo = Arrays.asList(myArray);

you say
String[] myArray = {"d", "c", "z", "q"};
List<String> foo = new ArrayList( Arrays.asList(myArray) );
foo.add("Halloween ");
System.out.println(foo);




Yours,
Bu.
 
Jerret Halter
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, very nice, and ArrayList are much nicer :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic