简体   繁体   English

将元素添加到双向链接列表中

[英]adding elements into the doubly linked list

this is my code for main class and doubly linked class and node class but when I run the program ,in the concole will show this"datastructureproject.DoublyLinkedList@19ee1ac" instead of the random numbers .please help me thanks! 这是我的主类和双链接类以及节点类的代码,但是当我运行该程序时,console中将显示此“ datastructureproject.DoublyLinkedList@19ee1ac”,而不是随机数。请帮助我,谢谢!

main class: 主类:

    public class Main {

    public static int getRandomNumber(double min, double max) {
        Random random = new Random();
        return (int) (random.nextDouble() * (max - min) + min);

    }

    public static void main(String[] args) {
        int j;
        int i = 0;
        i = getRandomNumber(10, 10000);
        DoublyLinkedList listOne = new DoublyLinkedList();

        for (j = 0; j <= i / 2; j++) {
            listOne.add(getRandomNumber(10, 10000));


        }
        System.out.println(listOne);

    }
}

doubly linked list class: 双链表类:

public class DoublyLinkedList {

private Node head ;
private Node tail;
private long size = 0;

public DoublyLinkedList() {
    head= new Node(0, null, null);
    tail = new Node(0, head, null);
}



public void add(int i){

head.setValue(i);
Node newNode = new Node();
head.setNext(newNode);
newNode.setPrev(head);
newNode = head;

}
public  String toString() {
StringBuffer result = new StringBuffer();
result.append("(head) - ");
Node temp = head;
while (temp.getNext() != tail) {
    temp = temp.getNext();
    result.append(temp.getValue() + " - ");
}
result.append("(tail)");
    return result.toString();
}
  }

and the node class is like the class that you have seen before (Node prev,Node next,int value) 节点类就像您之前见过的类(Node prev,Node next,int值)

edited: I have added toString method but will show null pointer exception for line "result.append(temp.getValue() + " - ");" 编辑:我已经添加了toString方法,但将显示行“ result.append(temp.getValue()+”-“);”的空指针异常 please help me thanks 请帮我谢谢

When you call System.out.println on an object it (kindly) calls the toString method of that object. 当您在对象上调用System.out.println时,它(会)调用该对象的toString方法。 If you haven't defined a toString for an object you will get the one defined by one of it's ancestors. 如果您尚未为对象定义toString ,则将获得由其祖先之一定义的对象。 In your case you aren't extending anything, so you will get the toString of Object - probably not what you want. 在您的情况下,您没有扩展任何内容,因此您将获得Object的toString-可能不是您想要的。

Try defining a toString() method in your class. 尝试在您的类中定义toString()方法。 In it you should probably loop over the nodes and build a String containing the required representation. 在其中,您可能应该遍历节点并构建一个包含所需表示形式的String

When you print the object, it executes it's .toString() method. 当您打印对象时,它执行.toString()方法。 What you see is the default toString implementation. 您将看到默认的toString实现。

You can override .toString to customize what get's printed - in your case, you'd probably loop over the items and create a comma-separated list of the numbers or something 您可以重写.toString以自定义要打印的内容-在您的情况下,您可能会遍历所有项目并创建以逗号分隔的数字或其他内容列表

running System.out.println(Object); 运行System.out.println(Object); will need to convert Object into a string. 将需要将Object转换为字符串。 It does this by executing the toString method. 它通过执行toString方法来实现。 If an object has not implemented toString the default implementation is used which returns the class name and its hashcode. 如果对象尚未实现toString ,则使用默认实现,该默认实现返回类名称及其哈希码。

you will need to either override the object and supply a suitable toString or loop through the elements and build your string prior to calling println yourself. 您将需要覆盖该对象并提供合适的toString或者在您自己调用println之前遍历元素并构建您的字符串。

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

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