gpt4 book ai didi

java - 在方法内创建节点

转载 作者:行者123 更新时间:2023-11-30 07:48:07 25 4
gpt4 key购买 nike

我正在尝试将此代码“转换”为创建节点项的方法。我知道我必须使用 for 循环,但我无法找到完成此操作的方法。

原始代码:

public class GenericLinkedListDemo
{
public static void main(String[] args)
{
LinkedList3<Entry> list = new LinkedList3<Entry>( );
Entry entry1 = new Entry(1);
list.addToStart(entry1);
Entry entry2 = new Entry(2);
list.addToStart(entry2);
Entry entry3 = new Entry(3);
list.addToStart(entry3);

}

到目前为止我所做的是在 GenericLinkedListDemo 中创建一个发送参数的方法:

public class GenericLinkedListDemo
{
public static void main(String[] args)
{
LinkedList3<Entry> list = new LinkedList3<Entry>( );
addToList(list, 7);

我的方法:

public static void addToList(LinkMaster<Entry> L, int n){
for (int i = n; i>0; i--) {
//This is where I want to put my "converted code"
}
}

我已经完成了创建节点(LinkMaster)的所有方法。我只是想知道如何使上面的这段代码以我只需要向代码发送参数的方式工作。

最佳答案

我猜你想要这样的东西

public static void addToList(LinkMaster<Entry> list, int n){//here n will determine number of entry node to be added
for (int i = n; i>0; i--) {
Entry entry = new Entry(i);
list.addToStart(entry);
}
}

如果你传递n = 7,那么7个入口节点将被添加到列表中。

关于java - 在方法内创建节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33664179/

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