Home arrow static arrow Java Programming [Archive] - Data being overwritten in my LinkedList
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Data being overwritten in my LinkedList
10 Duke Stars available
This topic has 11 replies on 1 page.

Posts:23
Registered: 8/4/03
Data being overwritten in my LinkedList  
Aug 4, 2004 4:45 PM



 
I have a linkedlist, called myHardware, consisting of different objects and linkedlist. I can add numerous objects, which is of type Component, and every time I add a new one no problem. It does no longer overwrite the old objects, which it did in the beginning. Now I add a LinkedList, myTemporaryPC, to the first LinkedList, no problem. But when I add myTemporaryPC again, this time with new data, the old data is overwritten.
what is the problem

I think it is some kind of missing new keyword, but I don't know where.

This is where I add the LinkedList
myHardware.addFirst(myTemporaryPC);	


let me know if you need more code
 

Posts:12,831
Registered: 2/22/00
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 5:07 PM (reply 1 of 11)



 

After you do that, what' s in the second item in the list?
 

Posts:23
Registered: 8/4/03
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 5:16 PM (reply 2 of 11)



 

After you do that, what' s in the second item in the list?

I'm not sure what list you're refering to so, here goes.

in list 1, the myHardware. if i first add a component, then a myTemporaryPC, a new component, a myTemporaryPC againg, the last myTemporaryPC overwrites the first one, making them the same.

nothing happens with the myTemporaryPC list, but i've tried using the myTemporaryPC.clear() method, to reset it. but with the result, that it just leaves and empty space in the list myHardware.
 

Posts:12,831
Registered: 2/22/00
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 7:24 PM (reply 3 of 11)



 
the myHardware list. You realize that when you call addFirst on that, everything that's in the list gets pushed up, right?
 

Posts:31,095
Registered: 4/30/99
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 8:48 PM (reply 4 of 11)



 
in list 1, the myHardware. if i first add a component,
then a myTemporaryPC, a new component, a myTemporaryPC
againg, the last myTemporaryPC overwrites the first
one, making them the same.

Most likely you are adding the same TemporaryPC object to the list twice. The fact that you mention a clear() method makes that very likely. Another possibility (less likely) is that you are storing data in static variables in the TemporaryPC class.

PC²
 

Posts:5,965
Registered: 5/17/03
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 10:26 PM (reply 5 of 11)



 
Most likely you are adding the same TemporaryPC object
to the list twice.

Yes the LinkedList (an any collection) stores references to objects (not the object values).

So you must do this,
Component myTemporaryPC = new Component(); // new object// add data to the object myTemporaryPC points tomyHardware.addFirst(myTemporaryPC); // store object//myTemporaryPC = new Component(); // another new object// add data to the the new object myTemporaryPC points tomyHardware.addFirst(myTemporaryPC); // store new object
 

Posts:5,965
Registered: 5/17/03
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 10:28 PM (reply 6 of 11)



 
I should have written "store object reference" because references are what collections hold.
 

Posts:23
Registered: 8/4/03
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 10:55 PM (reply 7 of 11)



 
So you must do this,
Component myTemporaryPC = new Component(); // new object// add data to the object myTemporaryPC points tomyHardware.addFirst(myTemporaryPC); // store object//myTemporaryPC = new Component(); // another new object// add data to the the new object myTemporaryPC points tomyHardware.addFirst(myTemporaryPC); // store new object

There seems to be a problem doing
Component myTemporaryPC = new Component();// andmyTemporaryPC = new Component();

since they both are linkedlists.
Or is it just me misunderstanding how to do as you say, i that case, could you be more specific?
 

Posts:5,965
Registered: 5/17/03
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 11:26 PM (reply 8 of 11)



 
Sorry I didn't read your post carefully enougth. You have a mix of Component and LinkedList objects in a LinkedList?

Anyway because all collections, including LinkedList, store references only and not the objects themselves. A common mistake is to store the same object reference twice in the belief that you have stored two objects. But you haven't. You have now two references pointing at the same object.
 

Posts:5,965
Registered: 5/17/03
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 11:38 PM (reply 9 of 11)



 
Say for example myTemporaryPC holds a LinkedList with Component objects. Now if you create a new LinkedList using the old like

LinkedList myTemporaryPC_1 = new LinkedList(myTemporaryPC);

The new LinkedList will contain the same references to Component objects as the first (it's called a shallow copy). If you change an object found in myTemporaryPC this change will show up in the new myTemporaryPC_1. To prevent this you'd have to copy all Components objects too (called a deep copy).
 

Posts:23
Registered: 8/4/03
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 11:49 PM (reply 10 of 11)



 
Okay how to make a deep copy?
 

Posts:5,965
Registered: 5/17/03
Re: Data being overwritten in my LinkedList  
Aug 4, 2004 11:58 PM (reply 11 of 11)



 
Okay how to make a deep copy?

You see to it that not the object references get copied but the objects themselves. In the general case it becomes a recursive process but in special cases you simplify things. Here's an article about it,

http://java.sun.com/developer/JDCTechTips/2001/tt0410.html
 
This topic has 11 replies on 1 page.