在方法中创建节点
Creating node inside a method
我正在尝试 "convert" 将此代码添加到将创建节点项的方法中。我知道我必须使用 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个入口节点将被添加到列表中。
我正在尝试 "convert" 将此代码添加到将创建节点项的方法中。我知道我必须使用 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个入口节点将被添加到列表中。