File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using asList with Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using asList with Generics" Watch "Using asList with Generics" New topic
Author

Using asList with Generics

Jerret Halter
Ranch Hand

Joined: Feb 03, 2006
Posts: 62
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 :-)


Jerret Halter<br /> <br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.<hr></blockquote>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jerret Halter
Ranch Hand

Joined: Feb 03, 2006
Posts: 62
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Feb 03, 2006
Posts: 62
ICIC thanks a bunch.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Jerret Halter
Ranch Hand

Joined: Feb 03, 2006
Posts: 62
Ah, very nice, and ArrayList are much nicer :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using asList with Generics