Results 1 to 12 of 12
Thread: A question on Arrays.asList()
- 02-06-2012, 10:30 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
A question on Arrays.asList()
So when I say list.add(100), in runtime I get an exception.Java Code:public class GreenHouseController { public static void main(String[] args) { Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2)); collection.add(34); Collection<Integer> list = Arrays.asList(55,56,57); list.add(100); } }
I can obviously see that there is a new ArrayList<Integer>, a big difference.
But can someone try to explain what is going on in here?
What does an Arrays.asList exactly return?
and how does it work with new ArrayList<Integer> ?
- 02-06-2012, 10:51 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: A question on Arrays.asList()
What is the exception?when I say list.add(100), in runtime I get an exception.
- 02-06-2012, 10:53 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: A question on Arrays.asList()
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at myPaackkage.TestClass.main(TestClass.java:18)
- 02-06-2012, 10:54 PM #4
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: A question on Arrays.asList()
( I changed the class name to TestClass, please do not pay attention to that. I did not have the file anymore, I just copy - pasted it to a new file. This is the correct exception. )
- 02-06-2012, 10:56 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: A question on Arrays.asList()
As always, start tracking down obscure behaviour by reading the API docs.
Arrays (Java Platform SE 6)...)What does an Arrays.asList exactly return?
http://docs.oracle.com/javase/6/docs...il.Collection)and how does it work with new ArrayList<Integer> ?
-----
The API docs are not always easy to follow. But they are the place to start, and from which to frame precise questions.
- 02-06-2012, 10:59 PM #6
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: A question on Arrays.asList()
public static <T> List<T> asList(T... a)
Returns a fixed-size list backed by the specified array.
I know this.
My question is, what is going on when I say new ArrayList<Integer>(Arrays.asList(1,2)); ?
Am I creating an ArrayList object, that READS the values from another List ? But a List is an interface ? So what does that asList exactly return, and how is it good for an ArrayList constructor ?
- 02-06-2012, 11:03 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: A question on Arrays.asList()
OK, an unsupported operation is just what it says ... an operation that you can't do for some reason!Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at myPaackkage.TestClass.main(TestClass.java:18)
The Collection classes are defined in quite a general way and, in particular circumstances, an operation might not always be possible. In this case the operation is adding an element (100).
According to the asList() docs I linked to before, the asList() method: "Returns a fixed-size list backed by the specified array." It's the "fixed size" part that is important. The array you get from asList() has a fixed size - like an ordinary array - and you cannot add elements to it. In that sense add(100) is unsupported.
Read the other link to see why the ArrayList(/**/) constructor results in an array list where adding elements is supported.
- 02-06-2012, 11:08 PM #8
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: A question on Arrays.asList()
So asList() returns an array that is referenced by type List ?The array you get from asList() has a fixed size
It is saying like:
But this doesn't compile ?Java Code:List iAmTryingToLearn = new String[5];
What I am basically trying to ask is:
If it returns an array, why is the return type List ? Can a List reffer to an array then ? If so, how ?
- 02-06-2012, 11:13 PM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: A question on Arrays.asList()
Yes, you can think of it that way. Personally, I (mostly) prefer the wording of the API docs over alternatives: the constructor "Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator." Which, I guess, amounts to reading what the argument list contains and adding them to the list being constructed.Am I creating an ArrayList object, that READS the values from another List ?
It is, but that doesn't change the fact that things will be done exactly as the API says they will be done.But a List is an interface ?
When you say "new ArrayList<Integer>(Arrays.asList(1,2));" you will get a new array list. It will have exactly the same elements as Arrays.asList(1,2) in exactly the same order. The newly created list will not be subject to any restrictions about being fixed size. (because the API doesn't say it will be)
a fixed size listSo what does that asList exactly return,
by telling the constructor what elements should go into the new list, and in what order.and how is it good for an ArrayList constructor ?
- 02-06-2012, 11:15 PM #10
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: A question on Arrays.asList()
Thanks for your time, greatly appreciated.
- 02-06-2012, 11:22 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: A question on Arrays.asList()
Sorry, I was speaking loosely in saying it returns an array. I meant it returns an instance of List. List is an interface and the docs don't say what sort of List it returns, just that it is a List instance of some sort or other (ArrayList, LinkedList, or something else whose type they don't tell us about).
So forget I mentioned array.
(I adopt the convention of using lower case letters when I'm talking about sorts of thing like a list, and upper case when I'm talking about a defined type like List. Likewise I'll say int[] if I mean an actual java array, but just say array - or list! - if I mean a bunch of things in order. Since nobody else uses this convention, it is basically useless - but it helps me keep my own thinking straight.)
- 02-06-2012, 11:23 PM #12
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
question about 2d arrays
By jaxber in forum New To JavaReplies: 2Last Post: 06-03-2011, 04:02 PM -
Arrays.asList(...) Problem
By plm-pusik in forum New To JavaReplies: 2Last Post: 09-18-2010, 01:12 AM -
about Arrays.asList source
By geniusxiayi in forum Advanced JavaReplies: 2Last Post: 04-13-2009, 07:12 PM -
question about arrays
By broganm1 in forum New To JavaReplies: 3Last Post: 02-13-2008, 02:29 AM -
Question about arrays
By nhlfan in forum New To JavaReplies: 4Last Post: 11-15-2007, 11:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks