Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Multiple instances of linked list

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: Multiple instances of linked list

    Quote Originally Posted by thedolphin13 View Post
    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.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Multiple instances of linked list

    Quote Originally Posted by thedolphin13 View Post
    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.

Similar Threads

  1. Merging Arrays Vs. Linked List.
    By Kumarrrr in forum Collections and Generics
    Replies: 1
    Last Post: March 1st, 2010, 03:20 AM
  2. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM
  3. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 11:40 AM
  4. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 12:48 PM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 07:23 PM