Using asList with Generics
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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>
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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>
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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>
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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>
| I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











