Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用while循环继续在链接列表中添加节点?_Java_Linked List - Fatal编程技术网

Java 如何使用while循环继续在链接列表中添加节点?

Java 如何使用while循环继续在链接列表中添加节点?,java,linked-list,Java,Linked List,我目前正在学习链表,我已经发现了编码链表的基础知识,我完全理解它们。但是,我预设了一定数量的节点,因此用户无法添加更多节点。如何实现while循环以保持循环并询问用户是否要添加另一段数据 以下是迄今为止我已经掌握的代码: public class List { public int x; public List ptr = null; } 上面是List的对象类。列表包含一个数据类型x和一个指针 import javax.swing.JOptionPane; public

我目前正在学习链表,我已经发现了编码链表的基础知识,我完全理解它们。但是,我预设了一定数量的节点,因此用户无法添加更多节点。如何实现while循环以保持循环并询问用户是否要添加另一段数据

以下是迄今为止我已经掌握的代码:

public class List {

    public int x;
    public List ptr = null;

}
上面是List的对象类。列表包含一个数据类型x和一个指针

import javax.swing.JOptionPane;

public class Main {

    public static void main(String[] args) {

        List front = new List();
        front.x = Integer.parseInt(JOptionPane.showInputDialog("Enter a value"));

        List l1 = new List();
        l1.x = Integer.parseInt(JOptionPane.showInputDialog("Enter a value"));

        List l2 = new List();
        l2.x = Integer.parseInt(JOptionPane.showInputDialog("Enter a value"));

        front.ptr = l1;
        l1.ptr = l2;

        printNodes(front);

    }

    public static void printNodes(List p) {

        while (p != null) {

            System.out.print(p.x + " ");
            p = p.ptr;

        }

    }

}
如您所见,我创建了3个节点,但您不能再添加了。我想要一些大致如下的东西:

boolean goAgain = true;
while (goAgain) {

    //create a new node
    String again = JOptionPane.showInputDialog("Add another node?");
    if (!again.equalsIgnoreCase("yes")) {
        goAgain = false;
    }

}
谢谢大家!

我是一名高中二年级学生,请使用我能理解的词汇。我不会说我是java noob,但我也不是专家。

好吧,我会清理你的while循环,将所有内容都包含在一个语句中。但那只是因为我懒惰

while (!JOptionPane.showInputDialog("Add another node?").equalsIgnoreCase("yes")) 
{
    //make a new node
}
至于创建新节点的代码,我建议从Java的List接口实现List类。如果您刚开始使用Java,您可以阅读有关它的内容,尽管这可能有点难以理解。作为对现有代码的注释,请参见:

List front = new List();
front.x = Integer.parseInt(JOptionPane.showInputDialog("Enter a value"));

比如说,您并不是在创建新节点,而是在创建列表类的新实例。在这种情况下,对象被标记为“front”,我的建议是阅读更多关于列表如何运行的信息。这是一个很好的例子。

为什么不在list类中添加一个名为add的函数,它执行add对话框?然后,在你的if里面!再次.equalsyes,调用list.add函数?JOptionPane.showInputDialogAdd另一个节点?.equalsSignoreCaseYes如果用户单击“取消”,将抛出NullPointerException。OP的代码也有这个问题。我现在不担心捕捉异常,我只是想掌握这个概念。此外,除了JOptionPane之外,我没有其他的导入。如果你想得到@Jon下列表的概念,我真的想看看我答案中的链接。他们会告诉你如何/何时使用列表