Results 1 to 6 of 6

Thread: PrintMethod

  1. #1
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    43
    Rep Power
    0

    Default PrintMethod

    Hi i'm tyring to print the contents of my linked list. I'm using nodes and within those nodes it hold String data. So i want to print out the data within the nodes. Whenever i do
    System.out.println(node1.data),
    it prints perfectly. But i'm tyring to use a method where it would loop through the list and print out the data for every node in it. when i run my print method i get results such as
    Node@15db9742
    Node@6d06d69c
    Node@7852e922.
    Here is my print method i created
    Java Code:
    public void print(){
    		Node<E> current = head;
    		while (current.next != null){
    			System.out.println(current.data);
    			current = current.next;
    			
    		}
    	}

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: PrintMethod

    Where is the data field of the Node class declared? I ask because the output you gave looks like you are printing nodes, not their data as shown in the code.

  3. #3
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    43
    Rep Power
    0

    Default Re: PrintMethod

    I kinda know where this problem is coming from i have a node class
    Java Code:
    public class Node<E> {
    	E data;
    	Node<E> next;
    	
    	public Node(E data, Node<E> next){
    		this.data = data;
    		this.next = next;
    	}
    	public Node(E data){
    		this.data = data;
    		next = null;
    	}
    	
    
    }
    and I have a SMethods class that just has a bunch of methods like my print method shown above
    Java Code:
    public class SMethods<E> {
    	Node<E> head;
    	int size;
    ... rest is methods and my Test class
    Java Code:
    public class HelloWorld<E> {
    	public static void main(String[] args){
    SMethods<Node> names = new SMethods<Node>();
    .....
    .....
    Node<String> California = new Node<String>("California");
    		Node<String> Florida = new Node<String>("Florida");
    		Node<String> Alabama = new Node<String>("Alabama");
    .....
    ....
                    names.addFirst(California);
    		names.addFirst(Florida);
    		names.addFirst(Alabama);
    
    
    ...
    names.print();
    And then we get the Node references.
    But when i switch SMethods to SMethods<String> and change my addFirst arguments to strings it prints out fine.
    But my main problem/question is there is when i change it to SMethods<String> there is no real link between the node that i made and my addFirst statment
    for example
    Node<String> California = new Node<String>("Sacramento");
    names.addFirst("sacramento");
    names would add the string data sacramento to an unnamed node in names, and the only way to reference that specific node would be to use index() or getIndex() something along those lines. i can't reference that node by its name. Like when i use SMethods<Node> i could reference my node by just using California. For example i could say California.next = Washington. or i could say names.addFirst(California) and it would add California the node and "Sacramento" the data. The hard part is just being able to print out the data when i use SMethods<Node>.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: PrintMethod

    Java Code:
       System.out.println(current.data);
    If data is of type Node that could explain the output: Node@<hexNumber>

    If you want a different output, override the toString() method of the Node class and have it return what you want to see.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    43
    Rep Power
    0

    Default Re: PrintMethod

    Overriding the toString() method doesn't seem to work with me, still getting the hexNumbers.
    Java Code:
    public String toString(){
    		Node<E> ptr = head;
    		String result = "";
    		while(ptr != null){
    			result += ptr.data + " ";
    			ptr = ptr.next;
    		}
    		return result;
    	}

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: PrintMethod

    The toString() method should be in the Node class and should return ONLY data about what is in that one instance of the Node class.

    You don't show what class the code in post#5 is in.
    If you don't understand my response, don't ignore it, ask a question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •