• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding Arrays.asList()

 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning all,

I have been reviewing Sybex OCA 8 study guide and at the end of the third chapter, there were some questions that used Arrays.asList(). So, I went to the googles to try and makes heads or tails of the method and couldn't make much sense of it.

I put the code below into my compiler:



Both println commands print the same:


run:
[a, b, c]
[a, b, c]
BUILD SUCCESSFUL (total time: 0 seconds)


Could someone explain the difference between the two List declarations on Lines 9 & 10?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis,
Great question! The difference is the first one makes a new ArrayList that starts with the data in the array. It is a full fledged ArrayList and you can add/remove elements. The second one "wraps" the array with ArrayList methods. You can get elements from it or change elements. However, you cannot add elements since the underlying array size is fixed.

Try adding this to your example and running it:
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Travis,

First thing to do when you are unfamiliar with a method, is have a look at the javadoc of this method. Here you'll find the javadoc for Arrays.toList.

So it creates a fixed-size list backed by the specified array. So there are 2 "difficult" words which need some explanation: fixed-size and backed by. A few code snippets will illustrate both, we'll start with fixed-size:line1 prints what you probably expected: [one, two, three]. But when executing line2, you'll get an UnsupportedOperationException because you can't add/remove elements to this list meaning you can't perform any operation which would change the size of this list => fixed-size

This code snippet illustrates the backed by the specified array part:Output:
[one, two, three] - [one, two, three]
[three, TWO, one] - [three, TWO, one]

From the above output you can see what's happening. Although you can't perform any operation which affects the size of the list, you can change elements in the list. And any change to an element in the list affects the array as well. And it works also the other way round: a change to an element of the array affects the element in the list as well.

With new ArrayList<String>(aCollection) you create a new list. This list is not fixed-size and is not backed by the specified array as illustrated in the following code snippet:Output:
[one, two, three] - [one, two, three]
[three, TWO, three] - [one, two, one, four]


Hope it helps!
Kind regards,
Roel
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply!

I put the code below my declarations and I got an "unsupportedOperationException"

After reading your reply it sounds like the second add() method (line 13) is the culprit. Since it sounds like it is still an array, it's fixed.



Produces


run:
[a, b, c, d]
[a, b, cc]
List one: 4 List two: 3
BUILD SUCCESSFUL (total time: 1 second)


That definitely clears up a lot!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Roberts wrote:Thank you for your reply!

I put the code below my declarations and I got an "unsupportedOperationException"

After reading your reply it sounds like the second add() method (line 13) is the culprit. Since it sounds like it is still an array, it's fixed.

Exactly! You got it now.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A follow up question, if I may:



I was expecting two.equals(g) to produce true, but it came back false. Does the second List declaration copy a new String[] g and then wrap it with list methods? It does not appear so, I changed a value in two and it changed in String[] g as well.

Output:
run:
aa
[aa, b, c]
false
BUILD SUCCESSFUL (total time: 0 seconds)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Roberts wrote:A follow up question, if I may

Of course! That's why this forum is for

Travis Roberts wrote:I was expecting two.equals(g) to produce true, but it came back false.

Why would an array be equal to a List? g IS-A array, two IS-A List, even if both have the same elements they will never be equal, just because the types are different. It's like having a cat and a dog with exactly the same name, both animals will also never be equal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic