Multiple instances of linked list
OK, this is driving me nuts! The are three classes: Node, LinkedList and Test.
Node is...the node:
class IntNode{
private int data; //variable to store an integer value.
private Node link; //variable to store an IntNode object.
public IntNode( int initData, Node initLink ){
data = initData;
link = initLink;
}
}//end class Node
the constructor is LinkedList (NOTE: public LinkedList is empty) and contains node position stuff like add before, get next, etc.:
class LinkedList{
public LinkedList( ){
}
public add(){
....adds nodes.....this works
}
}//end class LinkedList
class Test has main, takes in values and is supposed to split all the values into two seperate linked lists and sort them...
public class Test {
public static void main(String[] args) throws FileNotFoundException{
IntLinkedList list = new IntLinkedList();
list = getListFrom("project2.txt"); - this just adds nodes and works
IntLinkedList first = new IntLinkedList();
}
}//end class Test
I omitted some code for simplicity, the question I have is, when I add Nodes to list, it contains 29 elements, after I declare first, everything in list is gone...it's like they are the same thing. Why is that? How can I have a linked list list and a linked list first? Sorry for the stupidity.
Re: Multiple instances of linked list
Quote:
Originally Posted by
thedolphin13
OK, this is driving me nuts! The are three classes: Node, LinkedList and Test.
Node is...the node:
class IntNode{
private int data; //variable to store an integer value.
private Node link; //variable to store an IntNode object.
public IntNode( int initData, Node initLink ){
data = initData;
link = initLink;
}
}//end class Node
the constructor is LinkedList (NOTE: public LinkedList is empty) and contains node position stuff like add before, get next, etc.:
class LinkedList{
public LinkedList( ){
}
public add(){
....adds nodes.....this works
}
}//end class LinkedList
class Test has main, takes in values and is supposed to split all the values into two seperate linked lists and sort them...
public class Test {
public static void main(String[] args) throws FileNotFoundException{
IntLinkedList list = new IntLinkedList();
list = getListFrom("project2.txt"); - this just adds nodes and works
IntLinkedList first = new IntLinkedList();
}
}//end class Test
I omitted some code for simplicity, the question I have is, when I add Nodes to list, it contains 29 elements, after I declare first, everything in list is gone...it's like they are the same thing. Why is that? How can I have a linked list list and a linked list first? Sorry for the stupidity.
Because you just created a new linked list with nothing in it.
Unless your LinkedList class starts with 29 elements, it will have none to start with, and it looks like your declaration is the last line of code before the end.
Are you trying to copy it into first?
If so, perhaps clone it, but that only works with Objects, i.e. Integer, not primitives, i.e. int, and also there are so things you have to do which I can't quite recall.
Perhaps somehow bring in the LinkedList that you put the 29 elements in.
Re: Multiple instances of linked list
I thought list and first were LinkedList objects?
IntLinkedList list = new IntLinkedList(); and IntLinkedList first = new IntLinkedList();
Maybe I have this wrong or am not understanding. How would I declare list and first as seperate LinkedList objects?
Re: Multiple instances of linked list
they are. However, list and first are references to a linked list. The linked list that list is referring to is not the same, unless you specifically set it using clone() or something, the same as the linked list that first is referring to.
Re: Multiple instances of linked list
OK...so list and first reference the same LinkedList object, which explains why when I make a change to one, it shows on the other. SO...how do I use clone to make list, first and second (I'll be using that also) so they will be their own separate objects? Thanks for all your help, I've been working on this project for three days now and hit one obstacle right after another. Linked lists are not me cup o' tea.
Re: Multiple instances of linked list
Quote:
Originally Posted by
thedolphin13
OK...so list and first reference the same LinkedList object, which explains why when I make a change to one, it shows on the other. SO...how do I use clone to make list, first and second (I'll be using that also) so they will be their own separate objects? Thanks for all your help, I've been working on this project for three days now and hit one obstacle right after another. Linked lists are not me cup o' tea.
list and first won't reference the same object unless you set it to.