Home arrow static arrow Java Programming [Archive] - probs with LinkedList
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - probs with LinkedList
This topic has 31 replies on 3 pages.    1 | 2 | 3 | Next »

Posts:23
Registered: 8/4/03
probs with LinkedList  
Aug 3, 2004 1:20 PM



 
I've got a problem adding objects to a linked list. I can add an object without problems, but when i add a second object, the new object overwrites the data from the first element.

I have a class Component with a nested class CPU
public class Component{public String maker; model, speed, price; public class CPU{public CPU (String ma, String m, String s, int p)		{			maker = ma;			model = m;			speed = s;			price = p;							}}}

then i add it to my list using
	public void addNewCPU(String maker, String model, String speed, int price)	{		Component.CPU newCPU = comp.new CPU(maker, model, speed, price);		myHardware.addFirst(newCPU);	}


I can't see what goes wrong

thanks
Romaniuz
 

Posts:1,652
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 1:24 PM (reply 1 of 31)



 
LinkedList.addFirst() adds the specified element as the first element in the list effectively overwriting the element already there.

Use LinkedList.add() instead.

See the API

http://java.sun.com/j2se/1.4.2/docs/api/
 

Posts:24,036
Registered: 2/3/03
Re: probs with LinkedList  
Aug 3, 2004 1:28 PM (reply 2 of 31)



 
LinkedList.addFirst() adds the specified element as
the first element in the list effectively overwriting
the element already there.

No, it will push the other elements down the list.
LinkedList list = new LinkedList();list.addFirst(new Object());list.addFirst(new Object());System.out.println(list.size()); // 2

There's probably something else going on. Post the code where you instantiate your List...
 

Posts:24,036
Registered: 2/3/03
Re: probs with LinkedList  
Aug 3, 2004 1:30 PM (reply 3 of 31)



 
LinkedList.addFirst() adds the specified element as
the first element in the list effectively
overwriting
the element already there.

No, it will push the other elements down the list.

That wasn't the greatest example - here's a better one:
LinkedList list = new LinkedList();list.addFirst("one");list.addFirst("two");System.out.println(list); // [two, one]
 

Posts:1,652
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 1:39 PM (reply 4 of 31)



 
Yaw,

True, but I did say effectively not actually. Though perhaps that wasn't the best way to say what I was after.

java.util.LinkedList l = new java.util.LinkedList() ;String addme = "I am item #"  ;for(int i=0; i<10; i++){    System.out.println(addme + i) ;    l.addFirst(addme + i) ;} for(int i=0; i<l.size(); i++){    System.out.println(l.get(i)) ;}


Yields the following

I am item #0
I am item #1
I am item #2
I am item #3
I am item #4
I am item #5
I am item #6
I am item #7
I am item #8
I am item #9
I am item #9
I am item #8
I am item #7
I am item #6
I am item #5
I am item #4
I am item #3
I am item #2
I am item #1
I am item #0

Which coincides with what you were saying, but if he's always asking for item index 0, or he's always asking for the first element in the list (LinkedList.getFirst()) he'll get whatever he just pushed into the stack given the way his code was written.

Though I concede that I didn't say it very well. Haste makes waste, or at least for bad code.
 

Posts:23
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 1:49 PM (reply 5 of 31)



 
What I add to the list has the form
maker = inputFromKeyboard.readString("\n maker -> ");model = inputFromKeyboard.readString(" Model -> ");speed= inputFromKeyboard.readString(" speed-> ");price = inputFromKeyboard.readInt(" Price -> ");wareHouseObject.addNewCPU(maker, model, speed, price);


the files/classes i have are
Component.java Which you have seen a part of
UserInterface.java which is where I enter the data
WareHouse.java where the LinkedList is
RunMe.java which is used to start the program
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 1:54 PM (reply 6 of 31)



 
How are you determining it is overwitten?
 

Posts:1,652
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 2:01 PM (reply 7 of 31)



 
And dubwai hits the big question. That's why he makes the big bucks. :)
 

Posts:23
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 2:05 PM (reply 8 of 31)



 
I use this to show the elements in the list
	public void displayMyHardware()	{		ListIterator components = myHardware.listIterator();		while (components.hasNext())		{			System.out.println(components.next());		}	}	


When entering one element it looks like:
'Maker' Amd 'Model' Barton 'Speed' 2200 'Price' 100

if I enter a second object e.g. an Intel CPU like 'Maker' Intel 'Model' Celeron 'Speed' 2200 'Price' 120
What the program will print is:

'Maker' Intel 'Model' Celeron 'Speed' 2200 'Price' 120
'Maker' Intel 'Model' Celeron 'Speed' 2200 'Price' 120

which to me looks very much the same.

In the program I also have the possibility to add a Motherboard which will look like the following.
(attributes are marked with ' ')
'Maker' DFI 'Model' NF2 'Information' Ultra AL 'Price' 200
If i add this to the list when it already holds the CPUs then the result will look like:

'Maker' DFI 'Model' NF2 'Information' Ultra AL 'Price' 200
'Maker' DFI 'Model' NF2 'Speed' 2200 'Price' 200
'Maker' DFI 'Model' NF2 'Speed' 2200 'Price' 200

where the only attributes that is left unchanged is the one that was different
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 2:06 PM (reply 9 of 31)



 
Can you show us the constructor and variables part of Component.CPU's defintion?
 

Posts:1,652
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 2:08 PM (reply 10 of 31)



 
I may regret asking this, but post the code for the whole thing please. There's something niggling around the edges of my thoughts on this which Yaw or Dubwai (both of whom are smarter than me) will probably see instantly.
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 2:08 PM (reply 11 of 31)



 
And dubwai hits the big question. That's why he makes
the big bucks. :)

You mean at the transvestite strip club right?
 

Posts:1,652
Registered: 8/4/03
Re: probs with LinkedList  
Aug 3, 2004 2:11 PM (reply 12 of 31)



 
FLAG ON THE POST DUBWAI IS PENALIZED FOR OVER SHARING, TEN YARDS AND LOSS OF BEER.

And dubwai hits the big question. That's why he
makes
the big bucks. :)

You mean at the transvestite strip club right?
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 2:11 PM (reply 13 of 31)



 
There's something niggling around
the edges of my thoughts on this

Hey, buddy. There's no need for racial epithets.
 

Posts:13,769
Registered: 00-11-29
Re: probs with LinkedList  
Aug 3, 2004 2:12 PM (reply 14 of 31)



 
FLAG ON THE POST DUBWAI IS PENALIZED FOR OVER
SHARING, TEN YARDS AND LOSS OF BEER.

Beer would be good right now. It'd send me right over the edge. But I probably shouldn't drink at work.
 
This topic has 31 replies on 3 pages.    1 | 2 | 3 | Next »