Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

list - LinkedList Java traverse and print

I would really appreciate if you can help to answer to this question:

I have already created a custom linked list myself in a very standard way using Java. Below are my classes:

public class Node {

   private Object obj;
   private Node next;

   public Node(Object obj){
       this(obj,null);
   }

   public Node(Object obj,Node n){
       this.obj = obj;
       next = n;
   }

   public void setData(Object obj){
       this.obj = obj;
   }

   public void setNext(Node n){
       next = n;
   }

   public Object getData(){
       return obj;
   }

   public Node getNext(){
       return next;
   }

}




public class linkedList {
    private Node head;


    public linkedList(){
        head = null;
    }

    public void setHead(Node n){
        head = n;
    }

    public Node getHead(){
        return head;
    }


   public void add(Object obj){
       if(getHead() == null){
           Node tmp = new Node(obj);
           tmp.setNext(getHead());
           setHead(tmp);
       }else{
           add(getHead(),obj);
       }
   }


   private void add(Node cur,Object obj){
       if(cur.getNext() == null){
           Node tmp = new Node(obj);
           tmp.setNext(cur.getNext());
           cur.setNext(tmp);
       }else{
           add(cur.getNext(),obj);
       }
   }


}

Im trying to print value i have inserted into the list as below

public static void main(String[] args) {
        // TODO code application logic here
        Node l = new Node("ant");
        Node rat = new Node("rat");
        Node bat = new Node("bat");
        Node hrs = new Node("hrs");

        linkedList lst = new linkedList();
        lst.add(l);
        lst.add(rat);
        lst.add(bat);
        lst.add(hrs);



        Node tmp = lst.getHead();
        while(tmp != null){

            System.out.println(tmp.getData());
            tmp = tmp.getNext();

        }


    }

but the output i got from the IDE is

linklist.Node@137bd6a1
linklist.Node@2747ee05
linklist.Node@635b9e68
linklist.Node@13fcf0ce

why does it print out the reference but not the actual value of the string such as bat,ant,rat... ?

If i want to print out the actual value then what should i do?

Thank you very much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your linkedList class already creates the Nodes for you!

linkedList list = new linkedList();
list.add("foo");
list.add("bar");
Node tmp = lst.getHead();
while(tmp != null){
    System.out.println(tmp.getData());
    tmp = tmp.getNext();
}

Will print

foo
bar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...