• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 23951
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: 23951
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 :-)
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic