打印ArrayList的子列表(基于用户输入)会导致ArrayList为空

时间:2018-10-08 12:29:42

标签: java arraylist printing sublist empty-list

一个非常简单的待办事项列表,它要求输入,然后以ArrayList的形式打印出该列表,该列表分为多个部分(子列表)(我的视野很差,所以我必须使用大字体,当list太长了,问题是列表的末尾不在页面上。尽我所能使用home / end按钮快速查看页面,这不是最佳情况,我宁愿破坏ArrayList进入子列表,然后打印出子列表,每行一个,如下所示:

这是今天的待办事项清单:    [醒来,Walk狗,吃早餐]    [铺床,向后扫一下,学习Java]

import java.util.Scanner; 导入java.util.ArrayList;

/**
 * @author Troy
 *
 */
public class HelloWorld {


    public static void main(String[] args) {
        // I chose an ArrayList because the size does not have to be predetermined.
        ArrayList<String> to_do = new<String>ArrayList();
        System.out.println("What would you like to add to your to-do list?");
        Scanner user_input = new Scanner(System.in);
        //While the user_input still has entries, perform the following:
        while (user_input.hasNextLine()) {
            //Add next entry in the to-do list(user_input) to the ArrayList
            String input = user_input.nextLine();
            //If input = remove, remove the last item in the to_do list.(ArrayList)
            if ("remove".equals(input)) {
                if (to_do.size() > 0) {
                to_do.remove(to_do.size() -1);
            }}
            /**If the user types in "exit", when prompted for the next item in their
             * to_do list, close user_input, and print out... 
             */
            if ("exit".equals(input)) {
                user_input.close();
                System.out.println("Your to-do list is complete!");

                ArrayList<String> sect1 = new ArrayList<String>(to_do.subList(0, to_do.size()));                
                if (to_do.size() <= 5) {
                    System.out.println(sect1 + "\n");
                    break;
                }
                ArrayList<String> sect2 = new ArrayList<String>(to_do.subList(6, to_do.size()));
                if (to_do.size() > 5 && to_do.size() <=10) {
                    System.out.println(sect1 + "\n" + sect2);
                    break;
            }
            //If input does NOT equal "remove", add user_input to the to_do list.
                if (!"remove".equals(input)) {
                    to_do.add(input);
                }

            System.out.println("\n");
            /**Print the ArrayList called "to_do" split into sections AFTER writing, 
             * "Here is today's to-do list:"
             *  */
            System.out.println("Here is today's to-do list: " + "\n");
            if (to_do.size() <= 5) {
                System.out.println(sect1 + "\n");
            }
            if (to_do.size() > 5 && to_do.size() <=10) {
                System.out.println(sect1 + "\n" + sect2);
            }

        }
    }
}}

2 个答案:

答案 0 :(得分:0)

问题是这里的括号位置。行if (!"remove".equals(input)) {if ("exit".equals(input)) {块内。我移动了if语句: 导入java.util.ArrayList;     导入java.util.Scanner;

public class HelloWorld {


    public static void main(String[] args) {
        // I chose an ArrayList because the size does not have to be predetermined.
        ArrayList<String> to_do = new<String>ArrayList();
        System.out.println("What would you like to add to your to-do list?");
        Scanner user_input = new Scanner(System.in);
        //While the user_input still has entries, perform the following:
        while (user_input.hasNextLine()) {
            //Add next entry in the to-do list(user_input) to the ArrayList
            String input = user_input.nextLine();
            //If input = remove, remove the last item in the to_do list.(ArrayList)
            if ("remove".equals(input)) {
                if (to_do.size() > 0) {
                to_do.remove(to_do.size() -1);
            }}

            /**If the user types in "exit", when prompted for the next item in their
             * to_do list, close user_input, and print out... 
             */
            if ("exit".equals(input)) {
                user_input.close();
                System.out.println("Your to-do list is complete!");

                ArrayList<String> sect1 = new ArrayList<String>(to_do.subList(0, to_do.size()));                
                if (to_do.size() <= 5) {
                    System.out.println(sect1 + "\n");
                    break;
                }
                ArrayList<String> sect2 = new ArrayList<String>(to_do.subList(6, to_do.size()));
                if (to_do.size() > 5 && to_do.size() <=10) {
                    System.out.println(sect1 + "\n" + sect2);
                    break;
            }
            //If input does NOT equal "remove", add user_input to the to_do list.
            if (!"remove".equals(input) && !"exit".equals(input)) {
                to_do.add(input);
            }

            System.out.println("\n");
            /**Print the ArrayList called "to_do" split into sections AFTER writing, 
             * "Here is today's to-do list:"
             *  */
            System.out.println("Here is today's to-do list: " + "\n");
            if (to_do.size() <= 5) {
                System.out.println(sect1 + "\n");
            }
            if (to_do.size() > 5 && to_do.size() <=10) {
                System.out.println(sect1 + "\n" + sect2);
            }

        }
    }
}}

答案 1 :(得分:0)

正如其他发布者所述,您的代码存在问题是if块的错误嵌套。这会使您的to_do.add位于if ("exit".equals(input))块中,因此您的列表仍然为空。我建议使用IDE并让其重新缩进(格式化)您的代码,然后这个问题将变得更加明显。

但是除此之外,您的代码中还有另一个问题:您的sect1占用了subList(0, to_do.size()),这是您的整个列表。这将导致它在一行上打印整个列表,这就是您所看到的。我建议您改为使用循环并将列表以相等的方式划分。由于subList已返回列表,因此您也不必将其包装在另一个ArrayList中,并且可以直接打印。

所以我将您的代码更正为:

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

/**
 * @author Troy
 */
public class HelloWorld {
    public static void main(String[] args) {
        // I chose an ArrayList because the size does not have to be predetermined.
        List<String> toDo = new ArrayList<String>();
        System.out.println("What would you like to add to your to-do list?");
        Scanner userInput = new Scanner(System.in);

        // While the userInput still has entries, perform the following:
        while (userInput.hasNextLine()) {
            // Get the next line entered by the user
            String input = userInput.nextLine();

            //If input is "remove", remove the last item in the toDo list. (ArrayList)
            if ("remove".equals(input)) {
                if (toDo.size() > 0) {
                    toDo.remove(toDo.size() -1);
                }
            }
            /*
             * If the user types in "exit", when prompted for the next item in their
             * toDo list, close userInput, and print out... 
             */
            else if ("exit".equals(input)) {
                userInput.close();
                System.out.println("Your to-do list is complete!");
                System.out.println("Here is today's to-do list: ");

                final int perLine = 3;
                int i = 0;
                while(i < toDo.size()) {
                    // Print from the start of our current chunk (i)
                    //  to the end (i+3), or to the size of the list if our last chunk is smaller than "perLine".
                    System.out.println(
                        toDo.subList(i, Math.min(toDo.size(), i+perLine))
                    );
                    i+=perLine;
                }

                break;
            }
            /*
             * If input is neither "remove" nor "exit", add input to the list
             */
            else {
                toDo.add(input);
            }
        }
    }
}

我也将一些变量更改为camelCase而不是snake_case,就像Java中的惯例一样。