• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

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

Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic