打印 ArrayList 的子列表(基于用户输入的待办事项列表)会导致空 ArrayList



一个非常简单的待办事项列表,它要求输入,然后以ArrayList的形式打印出列表,分为几个部分(子列表((我的视力真的很差,所以我不得不使用大字体,当列表太长时,问题是,列表的末尾会跑出页面。 尽管我可以使用主页/结束按钮快速查看页面,但这不是最佳情况。 我宁愿将 ArrayList 分解为子列表,然后打印出子列表,每行一个,如下所示:

这是今天的待办事项清单: 【起床,遛狗,吃早餐】 [整理床铺,扫地,学习Java]

import java.util.Scanner; 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.
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);
}
}
}
}}

正如另一张海报已经指出的那样,您的代码的问题在于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);
}
}
}
}

我还将一些变量更改为驼峰而不是 snake_case,这是 Java 中的约定。

问题是括号在这里的位置。线路if (!"remove".equals(input)) {位于if ("exit".equals(input)) {块内。我移动了if语句: import java.util.ArrayList; import 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);
}
}
}
}}

最新更新