Strange things with java...

Discussion in 'Java' started by Roberto Nicastro, May 11, 2007.

  1. Hi all,
    i have this piece of code

    Suppose that lstA is List of 3 Objects

    List<Object> lstA = ...something;
    List<Object> lstB = lstA;
    ....
    [block A] //this is a block of code
    ....

    Inside block A i remove one item from lstA, so lstA become a List of 2
    Objects. Inside block A i never touch lstB.
    Why at the end of block A also lstB is with 2 elements? How is this
    possible?

    Thanks in advance

    R
     
    Roberto Nicastro, May 11, 2007
    #1
    1. Advertising

  2. Roberto Nicastro wrote:
    > Hi all,
    > i have this piece of code
    >
    > Suppose that lstA is List of 3 Objects
    >
    > List<Object> lstA = ...something;
    > List<Object> lstB = lstA;
    > ...
    > [block A] //this is a block of code
    > ...
    >
    > Inside block A i remove one item from lstA, so lstA become a List of 2
    > Objects. Inside block A i never touch lstB.
    > Why at the end of block A also lstB is with 2 elements? How is this
    > possible?


    Neither lstA nor lstB is a list. They are both reference variables that
    are initialized as pointers to the same List object.

    Although block A does not touch lstB, it does remove an element from the
    List it points to.

    Patricia
     
    Patricia Shanahan, May 11, 2007
    #2
    1. Advertising

  3. Roberto Nicastro wrote:

    > Suppose that lstA is List of 3 Objects
    >
    > List<Object> lstA = ...something;
    > List<Object> lstB = lstA;
    > ...
    > [block A] //this is a block of code
    > ...
    >
    > Inside block A i remove one item from lstA, so lstA become a List of 2
    > Objects. Inside block A i never touch lstB.
    > Why at the end of block A also lstB is with 2 elements? How is this
    > possible?


    Because lstA and lstB are pointers. All objects are. When you assign
    lstB = lstA, you make lstB point at the same list; you make lstB ==
    lstA. Perhaps what you wanted to do was make lstB a copy of lstA? Or
    perhaps you wanted to go further and make lstB a copy of lstA and all
    its members copies of the members in lstA? Have you thought about what
    happens if you change an object in either list?

    --
    Regards,

    Richard
     
    Richard Senior, May 11, 2007
    #3
  4. Roberto Nicastro

    Raghav Guest

    On May 11, 10:51 pm, Roberto Nicastro <> wrote:
    > Hi all,
    > i have this piece of code
    >
    > Suppose that lstA is List of 3 Objects
    >
    > List<Object> lstA = ...something;
    > List<Object> lstB = lstA;
    > ...
    > [block A] //this is a block of code
    > ...
    >
    > Inside block A i remove one item from lstA, so lstA become a List of 2
    > Objects. Inside block A i never touch lstB.
    > Why at the end of block A also lstB is with 2 elements? How is this
    > possible?
    >
    > Thanks in advance
    >
    > R


    Actually here you have got one list in memory and 2 references to
    that. So when u modify the list, it is reflected in either of
    references pointing to that List.
    HTH.
     
    Raghav, May 11, 2007
    #4
  5. Roberto Nicastro

    Eric Sosman Guest

    Roberto Nicastro wrote On 05/11/07 13:51,:
    > Hi all,
    > i have this piece of code
    >
    > Suppose that lstA is List of 3 Objects
    >
    > List<Object> lstA = ...something;
    > List<Object> lstB = lstA;
    > ...
    > [block A] //this is a block of code
    > ...
    >
    > Inside block A i remove one item from lstA, so lstA become a List of 2
    > Objects. Inside block A i never touch lstB.
    > Why at the end of block A also lstB is with 2 elements? How is this
    > possible?


    An analogy (I love analogies!) that may help you
    understand what others have pointed out: lstA is the
    number on the debit card in your wallet, and lstB is
    the same string of digits recorded in your password-
    protected on-line form-filler assistant. In [block A]
    you hand the card to a local merchant to pay for your
    new jPea, 150 silver splonders including VAT. Then
    you go to your computer, use the automatic form-filler
    to log in to your bank account, and check the balance.
    Are the 150 silver splonders still there to be spent?

    One bank account, two copies of the account number.
    One List instance, two references to it. Spend money
    with the debit card, and the balance change is visible
    when you use the copied number. Change the List via
    listA, and the change is visible through listB.

    --
     
    Eric Sosman, May 11, 2007
    #5
  6. On 11 Mag, 20:17, Richard Senior <> wrote:

    > lstA. Perhaps what you wanted to do was make lstB a copy of lstA? Or
    > perhaps you wanted to go further and make lstB a copy of lstA and all
    > its members copies of the members in lstA? Have you thought about what
    > happens if you change an object in either list?
    >
    > --
    > Regards,
    >
    > Richard


    I want to create an exact copy of lstA. So, to create a real copy i
    have to create a new List object

    List lstA = ...something
    List lstB = new ArrayList();

    for (Iterator it; it = lstA.iterator(); it.hasNext(); ){
    lstB.add(it.next());
    }

    I think this will work for me.
    Thank you all

    R
     
    Roberto Nicastro, May 12, 2007
    #6
  7. Roberto Nicastro wrote:

    > I want to create an exact copy of lstA. So, to create a real copy i
    > have to create a new List object
    >
    > List lstA = ...something
    > List lstB = new ArrayList();
    >
    > for (Iterator it; it = lstA.iterator(); it.hasNext(); ){
    > lstB.add(it.next());
    > }
    >
    > I think this will work for me.


    Yes, but bear in mind that the two lists, while they are different
    lists, contain the same elements. If you change an object in lstA, it
    also changes in lstB.

    Have a look at this
    http://forum.java.sun.com/thread.jspa?threadID=487186&messageID=2281852
    and do some searches on "shallow copy" and "deep copy".

    --
    Regards,

    Richard
     
    Richard Senior, May 12, 2007
    #7
  8. Roberto Nicastro writes:

    > I want to create an exact copy of lstA. So, to create a real copy i
    > have to create a new List object
    >
    > List lstA = ...something
    > List lstB = new ArrayList();
    >
    > for (Iterator it; it = lstA.iterator(); it.hasNext(); ){
    > lstB.add(it.next());
    > }
    >
    > I think this will work for me.


    There's an addAll method you could use instead of a loop of your own:

    List lstB = new ArrayList();
    lstB.addAll(lstA);

    Look it up in Javadoc.
     
    Jussi Piitulainen, May 12, 2007
    #8
  9. Jussi Piitulainen wrote:
    > Roberto Nicastro writes:
    >
    >> I want to create an exact copy of lstA. So, to create a real copy i
    >> have to create a new List object
    >>
    >> List lstA = ...something
    >> List lstB = new ArrayList();
    >>
    >> for (Iterator it; it = lstA.iterator(); it.hasNext(); ){
    >> lstB.add(it.next());
    >> }
    >>
    >> I think this will work for me.

    >
    > There's an addAll method you could use instead of a loop of your own:
    >
    > List lstB = new ArrayList();
    > lstB.addAll(lstA);
    >
    > Look it up in Javadoc.


    Or, even simpler:

    List lstB = new ArrayList(lstA);

    Patricia
     
    Patricia Shanahan, May 12, 2007
    #9
  10. Roberto Nicastro

    Chris Smith Guest

    Roberto Nicastro <> wrote:
    > List<Object> lstA = ...something;
    > List<Object> lstB = lstA;


    While you've got several explanations of what's going on, you haven't
    gotten much by way of how to make it act as you'd like. To do that, you
    could change the second line there to:

    List<Object> lstB = new ArrayList(lstA); // or other List implementation

    That would cause lstB to point to a different List, which has its
    CONTENTS initialized from the list that lstA points to. Then you could
    modify the list that lstA points to, and the list that lstB points to
    would be unchanged.

    --
    Chris Smith
     
    Chris Smith, May 12, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexey Smirnov

    IIS Security strange things

    Alexey Smirnov, Mar 1, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,848
    Alexey Smirnov
    Mar 3, 2004
  2. =?Utf-8?B?R2FyeQ==?=

    ASP.NET does strange things

    =?Utf-8?B?R2FyeQ==?=, May 3, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    454
    Steven Cheng[MSFT]
    May 5, 2005
  3. Replies:
    1
    Views:
    901
    Harish
    Jan 7, 2005
  4. Toby A Inkster

    OT: strange things that people search for

    Toby A Inkster, Feb 8, 2004, in forum: HTML
    Replies:
    10
    Views:
    718
    Matthias Gutfeldt
    Feb 9, 2004
  5. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    vs2005 publish website doing bad things, bad things

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    626
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Oct 25, 2006
Loading...

Share This Page