Joined: Mon 09-22-2003 3:29PM Posts: 4317 Location: Find out on irc
Source: Havener Center
yea so java sucks.. actually no linked lists suck.
so were doing a library type thing using linked lists, and to each node im adding an array. the problem is for some reason each time i add a new node it overwrites all the toher ones..
so say i add the first node with data 1, then show the data and it prints 1.
if i were to add a new node with data 2, then when i show the data it prints 2 2, and for 3 it prints 3 3 3 etc etc...
so itd be nice if someone could tell me what was wrong with my code.
public void addANodeToStart(String[] addData)
{
head = new ListNode(addData, head);
}
public void showList( )
{
ListNode position;
position = head;
while (position != null)
{
System.out.println(blagh);
position = position.link;
}
}
as far as me and my teacher and other people can tell its waht it needs to be...
if u know java good, and have some time and would like to help im at the havener info desk till bout 7.. or if you can tell me whats wrong from that excerpt thatd be great, bc i know for a fact the problem lies within those 2 classes
_________________ _______Notebook_________Gaming - Circa 2008ish proc___T8300____________Q6600 clock-----------2.4GHz----------- ram--------------4GB------------ vid__GF8600M GT 256MB__2xGF8800GTS 512MB sli hdd____160GB_______3.5TBw/parity on a 3ware card
Joined: Mon 04-15-2002 4:23PM Posts: 516 Location: Far, far away from Rolla (Colorado to be exact)
Source: Off Campus
Post your ListNode(String[], ListNode) constructor and where/how you are using the addANodeToStart method.
_________________ That's right. I'm gone. Rolla couldn't hold me. If you are still there, I know your pain. But I'm still going to laugh at you from far, far away.
Joined: Sun 09-12-2004 8:22PM Posts: 657 Location: somewhere
Source: Kappa Sigma
I'd imagine that his constructor looks very similar to this:
Code:
public ListNode(String[] newData, ListNode linkValue) { data = new String[newData.length]; for (int i=0;i<data.length-1;i++){ data[i]=newData[i]; } link = linkValue; }
I'd like to see the section where you make the call to addANodeToStart. I'm betting that that is where the error is going to be at since what you've shown is basically what was given by the teacher.
And why are you passing an array? According to the problem description you should be passing an object.
_________________ if you woke up as me everyday, you'd hate yourself too.
Joined: Mon 09-22-2003 3:29PM Posts: 4317 Location: Find out on irc
Source: Havener Center
meh should would could, my way works for eveything except for the adding node part... and array is just as much of an object as a book object, i just dont have to write another class with getters and setters for using an array
raman check ur pm
_________________ _______Notebook_________Gaming - Circa 2008ish proc___T8300____________Q6600 clock-----------2.4GHz----------- ram--------------4GB------------ vid__GF8600M GT 256MB__2xGF8800GTS 512MB sli hdd____160GB_______3.5TBw/parity on a 3ware card
Joined: Mon 04-15-2002 4:23PM Posts: 516 Location: Far, far away from Rolla (Colorado to be exact)
Source: Off Campus
UrLocalGuru wrote:
meh should would could, my way works for eveything except for the adding node part... and array is just as much of an object as a book object, i just dont have to write another class with getters and setters for using an array
After just a quick look and without trying anything, here is what I see. When you are adding new entries you are never creating a new string array (ie: never creating a new object which is why you are seeing x yy zzz ...You keep updating the data in the existing object and creating more references to it).
Hope that helps.
_________________ That's right. I'm gone. Rolla couldn't hold me. If you are still there, I know your pain. But I'm still going to laugh at you from far, far away.
Users browsing this forum: No registered users and 1 guest
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum