| Author |
ArrayList and shallow cloning
|
RaviNada Kiran
Ranch Hand
Joined: Jan 30, 2009
Posts: 528
|
|
I have made a ArrayList al , with 3 Objects in it .
second i made an ArrayList by cloning the first one that is al.
when i make any changes onto the second ArrayList (Obtained from the clone) .
The changes are not reflecting , why so (As i read that the clone() will produce only a shallow copy)
|
If you want something you never had do something which you had never done
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32674
|
|
|
Please explain more. If you clone a List you will get two Lists which initially have the same contents; adding to list no 1 will not add to list no 2, surely? That is how I read the API documentation.
|
 |
amitabh mehra
Ranch Hand
Joined: Dec 05, 2006
Posts: 98
|
|
|
See if this old thread here can help you understand it better.
|
 |
higherzl Harez
Greenhorn
Joined: Feb 22, 2009
Posts: 1
|
|
RaviNada Kiran wrote:I have made a ArrayList al , with 3 Objects in it .
second i made an ArrayList by cloning the first one that is al.
when i make any changes onto the second ArrayList (Obtained from the clone) .
The changes are not reflecting , why so (As i read that the clone() will produce only a shallow copy)
if and only if the Objects you added into is String
if not , the changes you made to the second ArrayList's elements, the first ArrayList changed too.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
higherzl Harez wrote:if and only if the Objects you added into is String
you must mean immutable. Integer, Long and any other immutable objects behave the same as String - once created they can never change.
if not , the changes you made to the second ArrayList's elements, the first ArrayList changed too.
Technically the ArrayList itself does not change, but its contents change. You are most definitely right about that.
An example:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: ArrayList and shallow cloning
|
|
|