copy objects java OOP

P

Piet den Dulk

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
 
T

Thomas Fritsch

Piet said:
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
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Piet said:
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());
}
 
O

Oscar kind

Daniel Sj?blom said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top