• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Using asList with Generics

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ICIC thanks a bunch.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, very nice, and ArrayList are much nicer :-)
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic