简体   繁体   English

LinkedList Java遍历和打印

[英]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. 我已经使用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 但我从IDE获得的输出是

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 非常感谢你

Your linkedList class already creates the Node s for you! 您的linkedList类已经为您创建了Node

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

You need to override the toString() method on Node : 您需要覆盖Node上的toString()方法:

@Override
String toString() {
  String val = // however you want to represent your Node
  return val;
}

and print the node: 并打印节点:

System.out.println(tmp);

Alternatively, have Node.getData() return a String if you know you are only going to store String s -- change the Object s to String in the Node definition 或者,如果您知道只存储String ,则让Node.getData()返回一个String - 在Node定义中将Object更改为String

Alternatively, you could use generics to specify the type of data inside the node 或者,您可以使用泛型来指定节点内的数据类型

UPDATE: after a little more thought (provoked by comments), I see something amiss: 更新:经过多一点思考(由评论引起),我看到了一些不妥之处:

tmp.getData() should return a string (based on Node's single parameter ctor and that you are passing strings), but the output is about references to Node objects. tmp.getData()应返回一个字符串(基于Node的单个参数ctor并且您正在传递字符串),但输出是关于Node对象的引用。 Is it possible that linkedList.add re-sets the Node 's obj member to a Node instead? 难道linkedList.add重新设置Node的OBJ成员的Node呢?

The LinkedList is of Objects, Objects toString method prints out the reference. LinkedList是Objects,Objects toString方法打印出来的引用。 If you want to print out the string you are going to have either change the LinkedList to a String/Generics or cast the print out like this: 如果要打印出字符串,您可以将LinkedList更改为String / Generics,或者将print打印出来,如下所示:

String output = (String)tmp.getData();
System.out.println(output);

(you can do this in one line if you want). (如果需要,可以在一行中执行此操作)。

Your getData() in Node class returns an Object type. Node类中的getData()返回Object类型。 You need to cast it into a String so that it gets printed by println() properly. 您需要将其强制转换为String,以便println()正确打印。

System.out.println((String)tmp.getData()); 的System.out.println((字符串)tmp.getData());

should do the trick for you. 应该为你做的伎俩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM