簡體   English   中英

Java ArrayList打印出來是否為空?

[英]Java ArrayList printing out empty?

我的問題很簡單。 當輸入== 1時發送x的值時,該方法應該將x的值添加到al ArrayList中。 它是這樣做的,但是當我嘗試打印al的值時,它將打印為空。

頭等艙:

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();
            }
        }
    }
}

二等艙:

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);
            }
        }
    }
}

結果:

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

我是Java的新手,所以如果我在其他部分中的代碼編寫不正確,或者缺少明顯的東西,請不要感到驚訝。 感到沮喪的是,我不知道自己在做什么,我很高興能得到所有的幫助。

這行:

IncompleteTasks incompletetasks = new IncompleteTasks();

聲明一個局部變量incompletetasks ,特定於當前對start()調用,並將其初始化為一個新的空任務列表。

這行:

start();

創建一個新的,單獨的調用start() ,因此將有一個單獨的incompletetasks變量,並且不會包含有關您一直添加了所有內容的incompletetasks變量的任何信息。

沒有真正的理由應該調用start()

解決此問題的最快方法是更改​​此設置:

while (input != 0){

對此:

mainloop: while (input != 0) {

(其中mainlooplabel ,為while循環起一個名稱,以便您可以引用它),幾乎每次都這樣:

start();

對此:

continue mainloop;

(大致表示“返回到名為mainloop的循環的mainloop ”)。

更好的解決方法是將您的方法分成較小的部分。 這將允許更清晰的代碼。 通常,通常最好的方法是不要有嵌套循環(盡管有很多例外,所以不要把它當作硬規則)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM