Java 打印ArrayList的子列表(基于用户输入的待办事项列表)会导致ArrayList为空
一个非常简单的待办事项列表,请求输入,然后以分为多个部分(子列表)的ArrayList的形式打印出该列表(我的视力很差,所以我不得不使用大字体,当列表变得太长时,问题是列表的末尾会从页面上消失。虽然我可以使用主页/结束按钮快速查看页面,但这不是一种最佳情况。我宁愿将ArrayList分为多个子列表,然后打印出子列表,每行一个子列表,如t他说: 以下是今天的待办事项: [醒来,遛狗,吃早餐] [整理床铺,扫地,学习爪哇语] 导入java.util.Scanner; 导入java.util.ArrayListJava 打印ArrayList的子列表(基于用户输入的待办事项列表)会导致ArrayList为空,java,arraylist,printing,sublist,empty-list,Java,Arraylist,Printing,Sublist,Empty List,一个非常简单的待办事项列表,请求输入,然后以分为多个部分(子列表)的ArrayList的形式打印出该列表(我的视力很差,所以我不得不使用大字体,当列表变得太长时,问题是列表的末尾会从页面上消失。虽然我可以使用主页/结束按钮快速查看页面,但这不是一种最佳情况。我宁愿将ArrayList分为多个子列表,然后打印出子列表,每行一个子列表,如t他说: 以下是今天的待办事项: [醒来,遛狗,吃早餐] [整理床铺,扫地,学习爪哇语] 导入java.util.Scanner; 导入java.util.Arra
/**
* @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);
}
}
}
}}
/**
*@作者特洛伊
*
*/
公共类HelloWorld{
公共静态void main(字符串[]args){
//我选择了ArrayList,因为大小不必预先确定。
ArrayList to_do=newArrayList();
System.out.println(“您想在待办事项列表中添加什么?”);
扫描仪用户输入=新扫描仪(System.in);
//当用户输入仍有条目时,执行以下操作:
while(user\u input.hasNextLine()){
//将待办事项列表中的下一项(用户输入)添加到ArrayList
String input=user_input.nextLine();
//如果输入=删除,则删除待办事项列表中的最后一项。(ArrayList)
如果(“删除”。等于(输入)){
如果(to_do.size()>0){
to_do.remove(to_do.size()-1);
}}
/**如果用户键入“退出”,则在提示用户输入下一项时
*要执行任务列表,请关闭用户输入,然后打印输出。。。
*/
如果(“退出”。等于(输入)){
用户_input.close();
System.out.println(“您的待办事项列表已完成!”);
ArrayList sect1=新的ArrayList(to_do.subList(0,to_do.size());
if(to_do.size()5&&to_do.size()这里的问题是括号的位置。行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);
}
}
}
}}
公共类HelloWorld{
公共静态void main(字符串[]args){
//我选择了ArrayList,因为大小不必预先确定。
ArrayList to_do=newArrayList();
System.out.println(“您想在待办事项列表中添加什么?”);
扫描仪用户输入=新扫描仪(System.in);
//当用户输入仍有条目时,执行以下操作:
while(user\u input.hasNextLine()){
//将待办事项列表中的下一项(用户输入)添加到ArrayList
String input=user_input.nextLine();
//如果输入=删除,则删除待办事项列表中的最后一项。(ArrayList)
如果(“删除”。等于(输入)){
如果(to_do.size()>0){
to_do.remove(to_do.size()-1);
}}
/**如果用户键入“退出”,则在提示用户输入下一项时
*要执行任务列表,请关闭用户输入,然后打印输出。。。
*/
如果(“退出”。等于(输入)){
用户_input.close();
System.out.println(“您的待办事项列表已完成!”);
ArrayList sect1=新的ArrayList(to_do.subList(0,to_do.size());
if(to_do.size()5&&to_do.size()这里的问题是括号的位置。行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);
}
}
}
}}
公共类HelloWorld{
公共静态void main(字符串[]args){
//我选择了ArrayList,因为大小不必预先确定。
ArrayList to_do=newArrayList();
System.out.println(“您想在待办事项列表中添加什么?”);
扫描仪用户输入=新扫描仪(System.in);
//当用户输入仍有条目时,执行以下操作:
while(user\u input.hasNextLine()){
//将待办事项列表中的下一项(用户输入)添加到ArrayList
String input=user_input.nextLine();
//如果输入=删除,则删除待办事项列表中的最后一项。(ArrayList)
如果(“删除”。等于(输入)){
如果(to_do.size()>0){
to_do.remove(to_do.size()-1);
}}
/**如果用户键入“退出”,则在提示用户输入下一项时
*要执行任务列表,请关闭用户输入,然后打印输出。。。
*/
如果(“退出”。等于(输入)){
用户_input.close();
System.out.println(“您的待办事项列表已完成!”);
ArrayList sect1=新的ArrayList(to_do.subList(0,to_do.size());
if(to_do.size()5&&to_do.size()正如另一张海报所述,您的代码的问题是if
块的嵌套不正确。这会导致to_do。将添加到if(“exit.equals(input))
块中,因此您的列表保持为空。我建议使用IDE并让它重新缩进(格式)你的代码,那么这个问题就会变得更加明显
但除此之外,代码中还有另一个问题:您的sect1
使用子列表(0,to_do.size())
这是您的整个列表。这将导致它将整个列表打印在您看到的一行上。我建议您改为使用循环,并以这种方式将列表等分。由于子列表
已返回列表,您也不必将其包装在另一个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);
}
}
}
}