Results 1 to 12 of 12
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default A question on Arrays.asList()

    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);
    	
    	}
    }
    So when I say list.add(100), in runtime I get an exception.

    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> ?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A question on Arrays.asList()

    when I say list.add(100), in runtime I get an exception.
    What is the exception?

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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)

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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. )

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A question on Arrays.asList()

    As always, start tracking down obscure behaviour by reading the API docs.

    What does an Arrays.asList exactly return?
    Arrays (Java Platform SE 6)...)

    and how does it work with new ArrayList<Integer> ?
    http://docs.oracle.com/javase/6/docs...il.Collection)

    -----

    The API docs are not always easy to follow. But they are the place to start, and from which to frame precise questions.

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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 ?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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)
    OK, an unsupported operation is just what it says ... an operation that you can't do for some reason!

    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.

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on Arrays.asList()

    The array you get from asList() has a fixed size
    So asList() returns an array that is referenced by type List ?

    It is saying like:

    Java Code:
    List iAmTryingToLearn = new String[5];
    But this doesn't compile ?

    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 ?

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A question on Arrays.asList()

    Am I creating an ArrayList object, that READS the values from another List ?
    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.

    But a List is an interface ?
    It is, but that doesn't change the fact that things will be done exactly as the API says they will be done.

    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)

    So what does that asList exactly return,
    a fixed size list

    and how is it good for an ArrayList constructor ?
    by telling the constructor what elements should go into the new list, and in what order.

  10. #10
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on Arrays.asList()

    Thanks for your time, greatly appreciated.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A question on Arrays.asList()

    Quote Originally Posted by fatabass View Post
    So asList() returns an array that is referenced by type List ?
    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.)

  12. #12
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on Arrays.asList()

    I will just add this:

    A limitation of Arrays.asList( ) is that it takes a best guess about the resulting type of the List, and doesn’t pay attention to what you’re assigning it to.
    "Thinking in Java" - and now I understand what this means.

Similar Threads

  1. question about 2d arrays
    By jaxber in forum New To Java
    Replies: 2
    Last Post: 06-03-2011, 04:02 PM
  2. Arrays.asList(...) Problem
    By plm-pusik in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 01:12 AM
  3. about Arrays.asList source
    By geniusxiayi in forum Advanced Java
    Replies: 2
    Last Post: 04-13-2009, 07:12 PM
  4. question about arrays
    By broganm1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2008, 02:29 AM
  5. Question about arrays
    By nhlfan in forum New To Java
    Replies: 4
    Last Post: 11-15-2007, 11:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •