• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in K&B SCJP 5: topic Converting Arrays to Lists to Arrays  RSS feed

Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I'm not sure if you can call it an erratum, but I found this a bit misleading.
In chapter 7 (Collections & Generics) page 559 the K&B book says

When you use the asList() method, the array and
the List become joined at the hip. When you update one of them, the other gets updated automatically.

then an example follows (see below) , and then
... they have both been updated with each other's changes. Wouldn't something like this behavior make a great exam question?

No erratum in the last sentence...

I looked up this posting with this errata list:

Nobody complained so far about this.

What I actually find a bit misleading, is that it says "joined at their hips".
First I thought this would be a special feature of the java.util.Arrays.asList() method.

But they are only linked, because the example only uses a reference, and does not make a new e.g. ArrayList.
The fact, that the changes are made both in the array and the list is not a result of using the asList method, but a result of using one object (the array) via two references.

Only to make this difference clear, I post some own code here:

First, the way the method was used in the K&B book:

As there is no ArrayList(LinkedList, whatsoever ...) object around, both - changes in the array as well as in the list - result in changes in both.
So the output is:

[7, 2, 3, 4, 8]
[7, 2, 3, 4, 8]

The last but one line is only for easier output.

This link comes only from addressing both with two references (array and list).
It is not a feature of the Arrays.asList method as the following example shows:

Now they are independent and not joined at the hip.
output is:
[1, 2, 3, 4, 8]
[7, 2, 3, 4, 5]

Anyone to comment on this?

And yes: the book IS worth every penny.


I hope I may post the original example from the book here. Otherwise just delete the following:
// Page 559

This produces
jimmy halim
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any misleading here

When you use:

Produce :

It's not the original java.util.ArrayList, it's inner-class from class Arrays.
The inner-class ArrayList has custom get & set method, it's connecting to the array variables
So when you change the content of array or list it'll change the same data.
With this Arrays$ArrayList class you cannot add/remove, it'll give you java.lang.UnsupportedOperationException

But when you use :

Produce :

It's a new Object, the original java.util.ArrayList class. And have no connection with the Array variables.

Maybe the book just not explicit say that the asList() method return a inner-class in class Arrays that declare like this

[ November 30, 2006: Message edited by: jimmy halim ]
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!