...输出:
线程“main”中的异常 java.lang.IndexOutOfBoundsException:在 java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source) at java.base/ 的长度为 1 的索引 1 越界jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source) at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source) at java.base/java.util.Objects.checkIndex(Unknown Source) at java。 base/java.util.ArrayList.get(Unknown Source) at HelloWorld.main(HelloWorld.java:27) ...输出
我的程序是一个待办事项清单,如下:
...Java..................................
导入 java .util.Scanner; 导入 java.util.ArrayList;
/**
* @author Troy
*
*/
public class HelloWorld {
/** A very simple to-do list that asks for input and then prints out the list
* in the form of an ArrayList: eg:
Here is today's to-do list:
[Wake up, Walk the dog,Eat breakfast, Make the bed]
*/
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
to_do.add(user_input.nextLine());
System.out.println("\n");
/**Print out the ArrayList(to_do) 4 elements at a time. */
for (int i = 0; i < 5; i++ ) {
System.out.println("Your list for today is: " + "\n" + to_do.get(i));
}
}
}
}
......Java...................................... ……………………
一旦我在程序的最后编写 to_do.get(i) ,我就会收到上述错误消息。
另一个额外的问题是如何在不影响最终 ArrayList 的输出的情况下结束 while 循环?
12345678_0001
相关分类