gpt4 book ai didi

java - 将数字添加到范围

转载 作者:行者123 更新时间:2023-12-01 10:42:45 24 4
gpt4 key购买 nike

我正在编写一个程序,可以将整数序列添加到范围中。我无法将此值添加到范围中。它应该运行方法 addNodeAfter,但它什么也不做。

然后,当我想显示范围时,我会在这一行得到一个 NullPointerException :

for (int i = 1; i <= manyNodes; i++){

有什么建议吗?

主要:

public class PDEMain {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.print("Enter a start number: ");
Integer startNum = input.nextInt();

System.out.print("Enter end number: ");
Integer endNum = input.nextInt();

System.out.print("Enter increment: ");
Integer increment = input.nextInt();

Range obj = new Range(startNum, endNum, increment);
System.out.println(obj);

obj.display();
}

}

范围:

public class Range implements Cloneable {

private Integer data; // holds the data
private Range link; //holds the link
Range head; //refers to head of linked list
private Integer manyNodes;
private Integer startValue;
private Integer endValue;
private Scanner input;

public Range(Integer data, Range link){
this.data = data;
this.link = link;
}

public Range(Integer data, Range link, Range head) {
this.data = data;
this.link = link;
this.head = head;
manyNodes++;
}


public Range(Integer start, Integer end,Integer increment){
if(start == null){
startValue = 0;
}
if(increment == null){
if(start < end){
increment++;
}else{
increment--;
}
}
for (int i = start; i <= end; i+= increment){
addNodeAfter(i);
System.out.println(i);
}

}

public Integer getData() {
return data;
}

public void setData(Integer data) {
this.data = data;
}

public Range getLink() {
return link;
}

public void setLink(Range link) {
this.link = link;
}

public Range getHead() {
return head;
}

public void setHead(Range head) {
this.head = head;
}

public void addNodeAfter(Integer element){

this.link = new Range(element, this.link);

}

public void display(){
Range cursor = head;

for (int i = 1; i <= manyNodes; i++){ // NPE on this line
System.out.print(cursor.getData() + " ");
cursor = cursor.getLink();
}
System.out.println("");
}

}

最佳答案

您已将 manyNodes 定义为 Integer,而不是 int。这意味着它的默认值为 null,而不是 0,并且您永远不会在代码中的任何位置设置该值。

当您尝试在 display() 方法中使用它作为控制变量进行循环时,JVM 在尝试拆箱 null 时将抛出 NPE .

一个快速解决方法是将类型更改为 int:

private int manyNodes;

这将解决立即的 NPE,但仍然不会显示任何内容,因为您实际上从未在调用的构造函数中增加 manyNodes 。这意味着 display() 方法中的 for 循环失败,并且从未真正打印任何数据。

我建议完全摆脱 headmanyNodes ,并按照以下方式重写您的 display() 方法:

public void display() {
Range cursor = getLink();
while (cursor != null) {
System.out.print(cursor.getData() + " ");
cursor = cursor.getLink();
}
System.out.println("");
}

请注意,由于您在此构造函数中添加内容的方式,这将“向后”输出数据:

public static void main(String[] args) throws Exception {
Range obj = new Range(1, 10, 1);
obj.display(); // prints 10 9 8 7 6 5 4 3 2 1
}

您可能想查看 existing linked-list implementation更好地了解它们通常是如何编写的。

关于java - 将数字添加到范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34364453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com