aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B SCJP 5: topic Converting Arrays to Lists to Arrays  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B SCJP 5: topic Converting Arrays to Lists to Arrays  " Watch "Doubt in K&B SCJP 5: topic Converting Arrays to Lists to Arrays  " New topic
Author

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

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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:
http://www.coderanch.com/t/257589/java-programmer-SCJP/certification/SCJP-Errata-Updated

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.


Yours,
Bu.

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


This produces


all events occur in real time
jimmy halim
Greenhorn

Joined: Nov 28, 2006
Posts: 15
I don't see any misleading here
CMIIW

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 ]

Knowledge is power<br />My Blog : <a href="http://jimmod.blogspot.com/" target="_blank" rel="nofollow">http://jimmod.blogspot.com/</a>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Doubt in K&B SCJP 5: topic Converting Arrays to Lists to Arrays