打印 ArrayList 的子列表(基于用户输入的待办事项列表)会导致 ArrayList 为空
Printing sublist of an ArrayList (To-Do List based on user input) results in empty ArrayList
一个很简单的待办事项列表,要求输入,然后以ArrayList的形式打印出列表,分成几个部分(子列表)(我的视力很差,所以我必须使用大字体,并且当列表太长时,问题是列表的末尾超出了页面。尽管我可以使用 home/end 按钮快速查看页面,但这不是最佳情况。我会而是将 ArrayList 分成子列表,并打印出子列表,每行一个,如下所示:
这是今天的待办事项:
【起床,遛狗,吃早餐】
[铺床,扫背,学习Java]
进口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);
}
}
}
}}
问题在于括号的位置。 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);
}
}
}
}}
正如另一位发帖人所说,您的代码的问题是 if
块的嵌套不正确。这会导致您的 to_do.add
位于 if ("exit".equals(input))
块内,因此您的列表仍然为空。我建议使用 IDE 并让它重新缩进(格式化)你的代码,然后这个问题会变得更加明显。
但除此之外,您的代码中还有另一个问题:您的 sect1
需要 subList(0, to_do.size())
,这是您的整个列表。这将导致它在一行上打印整个列表,这就是您所看到的。我建议您改为使用循环并将列表分成相等的块。由于subList
已经returns一个列表,你也不必再用另一个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 中的约定。
一个很简单的待办事项列表,要求输入,然后以ArrayList的形式打印出列表,分成几个部分(子列表)(我的视力很差,所以我必须使用大字体,并且当列表太长时,问题是列表的末尾超出了页面。尽管我可以使用 home/end 按钮快速查看页面,但这不是最佳情况。我会而是将 ArrayList 分成子列表,并打印出子列表,每行一个,如下所示:
这是今天的待办事项: 【起床,遛狗,吃早餐】 [铺床,扫背,学习Java]
进口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);
}
}
}
}}
问题在于括号的位置。 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);
}
}
}
}}
正如另一位发帖人所说,您的代码的问题是 if
块的嵌套不正确。这会导致您的 to_do.add
位于 if ("exit".equals(input))
块内,因此您的列表仍然为空。我建议使用 IDE 并让它重新缩进(格式化)你的代码,然后这个问题会变得更加明显。
但除此之外,您的代码中还有另一个问题:您的 sect1
需要 subList(0, to_do.size())
,这是您的整个列表。这将导致它在一行上打印整个列表,这就是您所看到的。我建议您改为使用循环并将列表分成相等的块。由于subList
已经returns一个列表,你也不必再用另一个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 中的约定。