This topic is 4820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a linked list(ItemList class) called "list" and another copy of it named "dupeList". When i do: list.getNext(); a boolean variable in my ItemList class called "getNextCalled" will turn to 'true'. Now... here is the problem: when i do list.getNext(), the boolean variable "getNextCalled" in "dupeList" also changes to 'true'. why??? Arent they different objects?? please help. im using java by the way

##### Share on other sites
well i could think of a couple of things ..

#1) your boolean variable is a static variable ... don't think it is though.

#2) the copy you made called "dupeList" is not a deep copy of the linked list it's a actual copy of the pointer. Basically both variables are referencing the same Linked List in memory. Create a new linked list and change the memory address the "dupeList" is pointing to.

Post some code too .. that way people can help more.

##### Share on other sites
If you did
ItemList list = new ItemList;ItemList dupeList=list;

then they are references to the same list.

##### Share on other sites
hmm
how else would you declare it ? so they arent referencing to the same thing?

##### Share on other sites
Please help~~ i'm trying to sort this list... and only this part is working incorrectly. If i can make them not point to the same thing then i'm done~~ and freeed.

##### Share on other sites
I'm an expert with this stuff, but one of the most common ways to do it is to override the method clone(). It's one of the methods in the Object class that every other class is inherited from. In this method you create a "deep copy", which is done by either clone() -ing all the objects in it, and / or by copying the values of the primitives. For my linked lists, I "deep copy" everything but the objects the the list is pointing to. Those I just do a regular assignment (this.pointedTo = copyFrom.pointedTo). That's what I understand, anybody please correct me if I'm wrong.

- Freaker

##### Share on other sites
When i use clone() it says it's not visible from class Object...

##### Share on other sites
Oh, I'm sorry! I completely forgot. In Object, clone() is declared as protected. You can override it to be public, and I haven't actually tried but I think you can make it package private or private.

- Freaker

##### Share on other sites
You could make a copy constructor:

ItemList::ItemList(const ItemList& copy)
{
m_field1 = copy.m_field1;
m_field2 = copy.m_field2;
// and so on
}

Then when you want a deep copy just do:

ItemList duplicateList = new ItemList(originalList);

##### Share on other sites
Quote:
 Original post by ll0377You could make a copy constructor:ItemList::ItemList(const ItemList& copy){ m_field1 = copy.m_field1; m_field2 = copy.m_field2; // and so on}Then when you want a deep copy just do:ItemList duplicateList = new ItemList(originalList);

Quote:
 Original post by choboNo1im using java by the way

Er... good job there sir.

To the OP: Unless you are doing this for learning purposes, please please use one of the library collections instead (LinkedList or ArrayList).

Of course, that still won't teach you about copying objects versus aliasing them, which you'll need to understand anyway... :) However, IMHO the Java (and Python, by extension... and I'm pretty sure they both got it from somewhere else, likely Smalltalk? :s) object model encourages you *not* to make separate copies of objects when possible. And with good reason: maintaining a greater objects of the same type and similar state is harder, just like maintaining copy-and-paste code is. In most cases, if you really need a separate object, you will be just as well off to make one "from scratch" with an existing constructor.

Of course, if you *do* have a need (and I assume it comes up for some people, but somehow I've never really run into it), you can override clone() (although there is some weirdness with how it behaves...), or indeed implement a "copy constructor" - for a linked list you may find it easier to use recursion, and of course in Java it looks more like:

public class ItemList {  private Object data;  private ItemList next;  // other stuff...  public ItemList(ItemList source) {    data = source.data; // notice I'm not "copying" this    if (source.next == null) next = null;    else next = new ItemList(source.next);  }}

This assumes a list implementation where the data is not actually contained within the list, but just referenced from there. That means you can have multiple lists referring to (subsets of) the same data, and won't lose the data if you destroy a single list. In that context, you would usually not want to copy the data items when you copy a list.

##### Share on other sites
Whoops! Sorry for the confusion, apparently I totally missed the part about it being Java. But still, if you want a deep copy you can still make a copy constructor. I think that would be easier to understand than implementing clone().