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 10 of 10

Thread: where to put the nodes into the array?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default where to put the nodes into the array?

    hi guys
    im implementing a basic singly linked list structure (Java class) it should allow the addition and deletion of a single (element in the linked list) at a specified position i. we can assume that each element is int.

    there is a code (I have couple of similar but this is better than at hers so far)

    3)
    public class Node{

    public int data;
    Node next;

    public Node getNextNode(Node n){
    return n;
    }

    public Node(int data){
    this.setData(data);
    }

    public Node(int data,Node next){
    this.setData(data);
    this.next=next;
    }

    public int getData() {

    return data;
    }

    public void setData(int data) {

    this.data = data;
    }
    this is to create the node, but struggling with where to put the nodes into the array?
    any advice
    thx


  2. #2
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to put the nodes into the array?

    You need to add some methods if you physically want to keep track of all the Nodes. First of all, you need a LinkedList which accepts Nodes' so you can keep track of every single node and a variable to keep track of the index of that certain node.
    LinkedList<Node> allNodes = new LinkedList<Node>();
    int index;

    Then, inside your constructor, you want to add a Node to the list of nodes.
    allNodes.add(this);
    index = allNodes.size()-1;

    Then, if you want to keep track of that certain index, you can make a method that returns the index.
    public int getIndex(){
    return index;
    }

    And that will return the index which holds all of that data.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: where to put the nodes into the array?


  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to put the nodes into the array?

    Quote Originally Posted by Norm View Post
    Honestly, I think it is the same problem. Same type of problem, and same user who's posting it.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: where to put the nodes into the array?

    Is that a question?

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to put the nodes into the array?

    Quote Originally Posted by SupportIsPower View Post
    You need to add some methods if you physically want to keep track of all the Nodes. First of all, you need a LinkedList which accepts Nodes' so you can keep track of every single node and a variable to keep track of the index of that certain node.
    LinkedList<Node> allNodes = new LinkedList<Node>();
    int index;

    Then, inside your constructor, you want to add a Node to the list of nodes.
    allNodes.add(this);
    index = allNodes.size()-1;

    Then, if you want to keep track of that certain index, you can make a method that returns the index.
    public int getIndex(){
    return index;
    }

    And that will return the index which holds all of that data.
    is it adding an array list?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: where to put the nodes into the array?

    What is the "it" you refer to?

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to put the nodes into the array?

    Quote Originally Posted by Norm View Post
    What is the "it" you refer to?
    why u just go somewhere and have fun?!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: where to put the nodes into the array?

    If you'd write a full sentence it would help those trying to help you.

    I'll take your suggestion. This thread is getting to be a waste of time.

  10. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to put the nodes into the array?

    Quote Originally Posted by Norm View Post
    If you'd write a full sentence it would help those trying to help you.

    I'll take your suggestion. This thread is getting to be a waste of time.
    yeap, there is too much question than suggestion. luck of academic literature

Similar Threads

  1. Replies: 2
    Last Post: January 21st, 2011, 02:07 AM
  2. JTree - Remove All Nodes
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: December 9th, 2010, 05:27 PM
  3. LinkedIntLists issue - how can i move links without creating nodes?
    By bh-chobo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 1st, 2010, 03:59 AM
  4. How to enumarete Network Nodes using Java
    By dilshadpaleri in forum Java Networking
    Replies: 0
    Last Post: September 7th, 2010, 06:45 AM
  5. Having trouble redirecting nodes
    By KingLane in forum Collections and Generics
    Replies: 6
    Last Post: October 19th, 2009, 06:46 PM