从double到int的可能有损转换;JAVA链表
目前,我正在使用链表进行练习,但代码有问题。下面的代码运行正常,但当我试图使用生成随机数的方法添加一些节点时,它给了我这个错误。在添加代码之前,请运行addingnow,正如您在main中看到的那样。也许我错过了什么。有人能帮我理解吗 另外,评论部分是我试图升级的主要部分从double到int的可能有损转换;JAVA链表,java,math,linked-list,nodes,adt,Java,Math,Linked List,Nodes,Adt,目前,我正在使用链表进行练习,但代码有问题。下面的代码运行正常,但当我试图使用生成随机数的方法添加一些节点时,它给了我这个错误。在添加代码之前,请运行addingnow,正如您在main中看到的那样。也许我错过了什么。有人能帮我理解吗 另外,评论部分是我试图升级的主要部分 import java.util.Random; class Node { private int value; private Node next = null; public Node(int
import java.util.Random;
class Node {
private int value;
private Node next = null;
public Node(int value) {
this.value = value;
}
public int getValue() { return this.value; }
public Node getNext() { return this.next; }
public void setNext(Node pNext) { this.next = pNext; }
}
public class linked {
private Node head;
private Node tail;
private int size;
public int getSize() { return this.size; }
public void insert (Node ele) {
if (this.head == null) {
this.tail = ele;
this.head = this.tail;
}
else {
this.tail.setNext(ele);
this.tail = ele;
}
this.size++;
}
@Override
public String toString() {
StringBuilder ret = null;
if ((this.head != null) && (this.tail != null)) {
ret = new StringBuilder("[Dimensione: " + this.size
+ ", Head: "
+ this.head.getValue()
+ ", Tail: "
+ this.tail.getValue()
+ "] Elementi: ");
Node tmp = this.head;
while (tmp != null) {
ret.append(tmp.getValue() + " -> ");
tmp = tmp.getNext();
}
ret.append("/");
}
return ret == null ? "[null]" : ret.toString();
}
public static void main (String args[])
{
linked ll = new linked();
System.out.println(ll);
for(int i=0; i<15; i++) {
Random rand = new Random();
double pazz = rand.nextInt(50) + 1;
ll.insert(new Node(pazz));
}
/*
ll.insert(new Node(10));
System.out.println(ll);
ll.insert(new Node(25));
System.out.println(ll);
ll.insert(new Node(12));
System.out.println(ll);
ll.insert(new Node(20));
System.out.println(ll);
*/
}
}
在这里,您将pazz设置为double。您应该将其设置为int。1-将pazz声明为int。2-将new Random移到循环之外为什么在为pazz赋值时将其声明为double,并将其传递给需要int的方法?将声明更改为int。
double pazz = rand.nextInt(50) + 1;