gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:52:51 26 4
gpt4 key购买 nike

一个非常简单的待办事项列表,要求输入,然后以 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 block 的嵌套不正确。这会导致您的 to_do.add 位于 if ("exit".equals(input)) block 内,因此您的列表仍为空。我建议使用 IDE 并让它重新缩进(格式化)您的代码,然后这个问题就会变得更加明显。

但除此之外,您的代码中还有另一个问题:您的 sect1 采用 subList(0, to_do.size()) 这是您的整个列表。这将导致它将整个列表打印在一行上,这就是您所看到的。我建议您使用循环并以这种方式将列表分成相等的 block 。由于 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中的约定。

关于java - 打印 ArrayList 的子列表(基于用户输入的待办事项列表)会导致空 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52702352/

26 4 0