Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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 列表中的节点_Java_List - Fatal编程技术网

Java 列表中的节点

Java 列表中的节点,java,list,Java,List,我看到一个关于链接列表的问题,在试图解决它时遇到了一个问题,现在我想不出来了。这里有两个类: if (part1.equals("add")) { test.addNext(num); while(!test.isEmpty()){ System.out.println(test.pop() + " "); }

我看到一个关于链接列表的问题,在试图解决它时遇到了一个问题,现在我想不出来了。这里有两个类:

            if (part1.equals("add"))
            {
                test.addNext(num);
                while(!test.isEmpty()){
                System.out.println(test.pop() + " ");

                }

            }
下面的类将设置向链表添加节点

import java.util.List;
import javax.xml.soap.Node;


    int count;

    Set() {
        front = null;
        count = 0;
    }

    boolean isEmpty() {
        return front==null;
    }



    int pop() {
        int x = front.x;
        front = front.next;
        count--;
        return x;
    }

}
我似乎不明白为什么当用户输入各种整数时,我不能得到一个要打印的列表。如果我在没有用户输入的情况下硬编码,比如:

test.addNext(1);
test.addNext(7);
test.addNext(3);

while(!test.isEmpty()){
System.out.println(test.pop() + " ");
}
我得到这个输出:

3 7 1
当我允许用户输入整数时,为什么不能得到相同的结果?按照程序当前的状态,以下是输出的样子:

Enter command: add 1
1 
Enter command: add 7
7 
Enter command: add 3
3 
Enter command: 
当我希望输出如下所示时:

Enter command: add 1
1
Enter command: add 7
7 1
Enter command: add 3
3 7 1
Enter command: 
test.addNext(1);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(7);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(3);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}
@Override
public String toString(){
    StringBuilder sb = new StringBuilder();
    LinkedNode current = front;
    while(current != null){
        sb.append(current.x + " "); //Not an ideal solution, but demonstrates the right idea.
        current = current.next;
    }
    return sb.toString();
}

打印列表的代码正在修改列表:

while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}
将从列表中删除项,直到其为空。这意味着每次打印时(添加每个新项目后),都会立即再次删除该项目。获取用户输入和硬编码值的情况之间的区别在于,在每个新用户输入之后打印,而仅在硬编码数据的末尾打印

因此,如果您这样编写代码,您会看到同样的问题:

Enter command: add 1
1
Enter command: add 7
7 1
Enter command: add 3
3 7 1
Enter command: 
test.addNext(1);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(7);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(3);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}
@Override
public String toString(){
    StringBuilder sb = new StringBuilder();
    LinkedNode current = front;
    while(current != null){
        sb.append(current.x + " "); //Not an ideal solution, but demonstrates the right idea.
        current = current.next;
    }
    return sb.toString();
}
如果在打印列表之前向用户询问这三个项目,那么问题就会消失

使用当前代码,如果不删除列表中的所有节点(不考虑在打印后重新添加它们),则无法检查列表中的所有节点。我建议编写一个
toString
方法,它将打印每个节点,并且不会删除任何内容。大概是这样的:

Enter command: add 1
1
Enter command: add 7
7 1
Enter command: add 3
3 7 1
Enter command: 
test.addNext(1);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(7);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(3);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}
@Override
public String toString(){
    StringBuilder sb = new StringBuilder();
    LinkedNode current = front;
    while(current != null){
        sb.append(current.x + " "); //Not an ideal solution, but demonstrates the right idea.
        current = current.next;
    }
    return sb.toString();
}
或者,您可以实现
get(int)
size()
方法(或迭代器),并执行类似这样的循环,而不是当前的
while
循环:

for(int i = 0; i < test.size(); i++){
    System.out.println(test.get(i) + " ");
}
for(int i=0;i
打印列表的代码正在修改列表:

while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}
将从列表中删除项,直到其为空。这意味着每次打印时(添加每个新项目后),都会立即再次删除该项目。获取用户输入和硬编码值的情况之间的区别在于,在每个新用户输入之后打印,而仅在硬编码数据的末尾打印

因此,如果您这样编写代码,您会看到同样的问题:

Enter command: add 1
1
Enter command: add 7
7 1
Enter command: add 3
3 7 1
Enter command: 
test.addNext(1);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(7);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(3);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}
@Override
public String toString(){
    StringBuilder sb = new StringBuilder();
    LinkedNode current = front;
    while(current != null){
        sb.append(current.x + " "); //Not an ideal solution, but demonstrates the right idea.
        current = current.next;
    }
    return sb.toString();
}
如果在打印列表之前向用户询问这三个项目,那么问题就会消失

使用当前代码,如果不删除列表中的所有节点(不考虑在打印后重新添加它们),则无法检查列表中的所有节点。我建议编写一个
toString
方法,它将打印每个节点,并且不会删除任何内容。大概是这样的:

Enter command: add 1
1
Enter command: add 7
7 1
Enter command: add 3
3 7 1
Enter command: 
test.addNext(1);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(7);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}

test.addNext(3);
while(!test.isEmpty()){
    System.out.println(test.pop() + " ");
}
@Override
public String toString(){
    StringBuilder sb = new StringBuilder();
    LinkedNode current = front;
    while(current != null){
        sb.append(current.x + " "); //Not an ideal solution, but demonstrates the right idea.
        current = current.next;
    }
    return sb.toString();
}
或者,您可以实现
get(int)
size()
方法(或迭代器),并执行类似这样的循环,而不是当前的
while
循环:

for(int i = 0; i < test.size(); i++){
    System.out.println(test.get(i) + " ");
}
for(int i=0;i
你能解释一下为什么我在没有用户输入的情况下硬编码整数时,它会显示所有添加的数字吗?我很难理解编码值与从用户处获取值之间的区别。@mike1319我已在答案中添加了这一部分。基本上,区别在于打印的时间。在第一种情况下,将所有三个值读入列表,然后打印它。在用户输入的情况下,您可以在添加每个新项目后打印列表。现在这就非常有意义了。感谢您花时间向我详细解释这一点。您介意解释一下为什么我在没有用户输入的情况下硬编码整数,它会显示所有添加的数字吗?我很难理解编码值与从用户处获取值之间的区别。@mike1319我已在答案中添加了这一部分。基本上,区别在于打印的时间。在第一种情况下,将所有三个值读入列表,然后打印它。在用户输入的情况下,您可以在添加每个新项目后打印列表。现在这就非常有意义了。感谢您花时间向我详细解释这一点。