简体   繁体   English

Java ArrayList打印出来是否为空?

[英]Java ArrayList printing out empty?

My problem is simple. 我的问题很简单。 When sending the value of x when input == 1, the method is supposed to add the value of x into the al ArrayList. 当输入== 1时发送x的值时,该方法应该将x的值添加到al ArrayList中。 It does this, but when I attempt to print the values of al , it prints out empty. 它是这样做的,但是当我尝试打印al的值时,它将打印为空。

First class: 头等舱:

import java.util.Scanner;

public class ToDo {

    Scanner s = new Scanner(System.in);
    Scanner g = new Scanner(System.in);

    void start() {
        IncompleteTasks incompletetasks = new IncompleteTasks();
        CompleteTasks completetasks = new CompleteTasks();
        AllTasks alltasks = new AllTasks();
        int input = 999;
        String x = "";

        while (input != 0) {

            System.out.println("What would you like to do? Type '0' to cancel");
            System.out.println();
            System.out.println("1. Add a task"); // done
            System.out.println("2. View current tasks");
            System.out.println("3. Delete a task"); // done
            input = s.nextInt();
            if (input == 1) {
                while (!x.equals("quit")) {
                    System.out.print("Enter a task: (Type 'quit' to finish! ");
                    x = g.nextLine();
                    if (x.equals("quit")) {
                        start();
                    }
                    else {
                        incompletetasks.IncompleteTasksAdd(x);
                    }

                }

            }
            else if (input == 2) {
                System.out.println("\t1. All  Tasks");
                System.out.println("\t2. Complete Tasks");
                System.out.println("\t3. Incomplete Tasks");
                input = s.nextInt();
                if (input == 1) {
                    alltasks.AllTasks();
                }
                else if (input == 2) {
                    completetasks.CompleteTasks();
                }
                else if (input == 3) {
                    incompletetasks.IncompleteTasksDisplay();
                }
                else if (input == 0) {
                    System.exit(0);
                }

                else {
                    System.out.println("\t\t\t\tInvalid choice! Try again!");
                    start();
                }
            }

            else if (input == 3) {
                System.out.println("hello");
                x = s.nextLine();
                incompletetasks.IncompleteTasksDelete(x);
            }
            else if (input == 0) {
                System.exit(0);
            }
            else {
                System.out.println("\t\t\t\tInvalid choice! Try again!");
                start();
            }
        }
    }
}

Second Class: 二等舱:

import java.util.ArrayList;
import java.util.Scanner;

public class IncompleteTasks {

    int counter = 0;
    Scanner g = new Scanner(System.in);
    ArrayList<String> al = new ArrayList<String>();

    void IncompleteTasksDisplay() {
        System.out.println("----------------------------------------------------------");
        for (String f : al) {
            counter++;
            System.out.println(f);
            // System.out.println(counter+". " +al.get(f));
        }
        System.out.println("----------------------------------------------------------");

    }

    void IncompleteTasksAdd(String x) {
        al.add(x);
        System.out.println("\tTask Added!");
    }

    void IncompleteTasksDelete(String x) {
        for (int k = 0 ; k < al.size() ; k++) {
            if (al.get(k) == x) {
                al.remove(x);
            }
        }
    }
}

Result: 结果:

What would you like to do? Type '0' to cancel

1. Add a task
2. View current tasks
3. Delete a task
1
Enter a task: (Type 'quit' to finish! Test
Task Added!
Enter a task: (Type 'quit' to finish! Test 1
Task Added!
Enter a task: (Type 'quit' to finish! Test 2
Task Added!
Enter a task: (Type 'quit' to finish! quit
What would you like to do? Type '0' to cancel

1. Add a task
2. View current tasks
3. Delete a task
2
1. All  Tasks
2. Complete Tasks
3. Incomplete Tasks
3
----------------------------------------------------------
----------------------------------------------------------
What would you like to do? Type '0' to cancel

1. Add a task
2. View current tasks
3. Delete a task

I am new to java, so don't be surprised if I have bad coding in other sections or if I'm missing something very obvious. 我是Java的新手,所以如果我在其他部分中的代码编写不正确,或者缺少明显的东西,请不要感到惊讶。 I appreciate all the help I can get, as I'm stumped, I don't know what I'm doing wrong. 感到沮丧的是,我不知道自己在做什么,我很高兴能得到所有的帮助。

This line: 这行:

IncompleteTasks incompletetasks = new IncompleteTasks();

declares a local variable incompletetasks , specific to the current call to start() , and initializes it to a new empty list of tasks. 声明一个局部变量incompletetasks ,特定于当前对start()调用,并将其初始化为一个新的空任务列表。

This line: 这行:

start();

creates a new, separate call to start() , which will therefore have a separate incompletetasks variable, and will not have any information about the incompletetasks variable that you've been adding everything to. 创建一个新的,单独的调用start() ,因此将有一个单独的incompletetasks变量,并且不会包含有关您一直添加了所有内容的incompletetasks变量的任何信息。

There's no real reason that start() should need to call itself. 没有真正的理由应该调用start()

The quickest way to fix this is to change this: 解决此问题的最快方法是更改​​此设置:

while (input != 0){

to this: 对此:

mainloop: while (input != 0) {

(where mainloop is a label , giving the while-loop a name so that you can refer to it) and almost every occurrence of this: (其中mainlooplabel ,为while循环起一个名称,以便您可以引用它),几乎每次都这样:

start();

to this: 对此:

continue mainloop;

(meaning roughly, "go back to the beginning of the loop named mainloop "). (大致表示“返回到名为mainloop的循环的mainloop ”)。

A better fix is to split out your method into smaller parts. 更好的解决方法是将您的方法分成较小的部分。 That will allow for much clearer code. 这将允许更清晰的代码。 In general, it's usually best for a method not to have nested loops (though there are plenty of exceptions, so don't take that as a hard rule). 通常,通常最好的方法是不要有嵌套循环(尽管有很多例外,所以不要把它当作硬规则)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM