copy objects java OOP

Discussion in 'Java' started by Piet den Dulk, Feb 4, 2004.

  1. Dear Java programmers,

    I'm have a linked list that works fine. But When I try to make a copy of
    that list, the copy still references to the original. I allready have tried
    a copy contructor in my classes but this doesn't work because the classes
    themselves holds classes. Like al listNode holds a nexNode from thesame
    type.

    I need the copy from an object because I want to put it as an argument in a
    recursive(backtring) call. And I want to keep the original. How can I simple
    copy my list and have it's own content. It is really confusing me. Who can
    help me out and tell in simple words.

    best regards,
    Piet den Dulk
     
    Piet den Dulk, Feb 4, 2004
    #1
    1. Advertising

  2. Piet den Dulk wrote:

    >Dear Java programmers,
    >
    >I'm have a linked list that works fine. But When I try to make a copy of
    >that list, the copy still references to the original. I allready have tried
    >a copy contructor in my classes but this doesn't work because the classes
    >themselves holds classes. Like al listNode holds a nexNode from thesame
    >type.
    >
    >I need the copy from an object because I want to put it as an argument in a
    >recursive(backtring) call. And I want to keep the original. How can I simple
    >copy my list and have it's own content. It is really confusing me. Who can
    >help me out and tell in simple words.
    >
    >best regards,
    >Piet den Dulk
    >
    >
    >

    Piet,

    make your linkedList and/or listNode class implement the Cloneable
    interface, and write a clone() method for them, which does the copying.
    See http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Cloneable.html for
    more info.

    Thomas
    ______________________________________________________
    Thomas <dot> Fritsch <at> ops <dot> de
     
    Thomas Fritsch, Feb 4, 2004
    #2
    1. Advertising

  3. Piet den Dulk wrote:
    > Dear Java programmers,
    >
    > I'm have a linked list that works fine. But When I try to make a copy of
    > that list, the copy still references to the original. I allready have tried
    > a copy contructor in my classes but this doesn't work because the classes
    > themselves holds classes. Like al listNode holds a nexNode from thesame
    > type.
    >
    > I need the copy from an object because I want to put it as an argument in a
    > recursive(backtring) call. And I want to keep the original. How can I simple
    > copy my list and have it's own content. It is really confusing me. Who can
    > help me out and tell in simple words.


    As a quick reminder, you cannot access objects directly in java, only
    references to objects. Besides clone() that was mentioned, why not
    simply create a new list and copy the contents, instead of copying the
    Nodes. Example using the standard java.util.List interface (which you
    should implement, if you want your LinkedList to be really useful)

    List original; // original list
    List copy = new LinkedList(); // copy list

    for (Iterator i=original.iterator(); i.hasNext();)
    {
    copy.add(i.next());
    }

    --
    Daniel Sjöblom
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Feb 4, 2004
    #3
  4. Piet den Dulk

    Oscar kind Guest

    Daniel Sj?blom <_nospam> wrote:
    > List original; // original list
    > List copy = new LinkedList(); // copy list
    >
    > for (Iterator i=original.iterator(); i.hasNext();)
    > {
    > copy.add(i.next());
    > }


    Or simply:
    copy.addAll(original);

    Remember, a List is also a Collection.


    Oscar

    --
    Oscar Kind http://home.hccnet.nl/okind/
    Java/J2EE Developer email available on website
     
    Oscar kind, Feb 5, 2004
    #4
    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. Anonymous

    OOP in C++: Objects failing

    Anonymous, Jan 22, 2005, in forum: C++
    Replies:
    8
    Views:
    2,944
    Alan Krueger
    Jan 23, 2005
  2. Alex
    Replies:
    2
    Views:
    1,233
  3. rdc02271
    Replies:
    24
    Views:
    741
    Jacek Dziedzic
    Dec 27, 2005
  4. Vysakh P Pillai
    Replies:
    4
    Views:
    305
    Ben Pope
    Feb 27, 2006
  5. Replies:
    26
    Views:
    2,119
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page