gpt4 book ai didi

java - 用数字 1000-9999 填充链接列表

转载 作者:行者123 更新时间:2023-12-01 08:54:13 26 4
gpt4 key购买 nike

因此,我需要用数字 1000-9999 填充名为 Candidate 的链接列表。我知道如何对带有 foo 循环的数组执行此操作,我假设它与这里类似。

我有一个名为 setInfo 的 setter 方法,以及在节点类中创建的 set link 方法,它们在这里。

public void setInfo(int info){ //Info Setter
this.info = info;
}
public void setLink(LLIntegerNode link){ //Link setter
this.link = link;
}

我的尝试就在这里

LLIntegerNode candidate;   //Node class and Linked List named candidate
for(int j =9999; j >=1000; j--){
LLIntegerNode canNode = new LLIntegerNode(j, null); //new node
candidate.setInfo(canNode);
}

出了点问题,我需要设置所有数字以填充到这个新节点中,但我不知道如何正确执行...

我在 setInfo 处收到错误,它表示类型 LLIntegerNode 中的方法 setInfo(int) 不适用于参数 (LLIntegerNode)

这是我的 LLIntegerNode 类

public class LLIntegerNode{

private int info; // info inside node
private LLIntegerNode link; //create a link for nodes

public LLIntegerNode(int info, LLIntegerNode link){ //Constructor
this.info = info;
this.link = link;
}

public void setInfo(int info){ //Info Setter
this.info = info;
}

public int getInfo(){ //Info Getter
return info;
}

public void setLink(LLIntegerNode link){ //Link setter
this.link = link;
}

public LLIntegerNode getLink(){ //Link getter
return link;
}
}

最佳答案

您需要有一个光标才能将元素添加到链接列表中。否则,您只会覆盖列表中的相同元素。

LLIntegerNode head = new LLIntegerNode(9999, null);
LLIntegerNode cursor = head;
for(int j = 9998; j >= 1000; j--){
LLIntegerNode canNode = new LLIntegerNode(j, null); //new node
cursor.setLink(canNode);
cursor = canNode;
}

关于java - 用数字 1000-9999 填充链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42170135/

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