• 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 ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

ArrayList and shallow cloning

 
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if this old thread here can help you understand it better.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic