【发布时间】:2023-04-09 05:02:01
【问题描述】:
我正在尝试通过扫描仪输入将数字保存到数组中,但是
“java.lang.ArrayIndexOutOfBoundsException”不断弹出。
这是我的代码:
for (int counter = 1;1==1;counter++) {
int bucky[] = new int[counter];
int scan = oof.nextInt();
bucky[counter] = scan;
if (counter == 5) {
System.out.println(bucky);
}
谁能解释我做错了什么或者我在思考过程中哪里出错了?
【问题讨论】:
-
数组的第一个索引是什么?假设它的长度为 1,它的最后一个索引是多少? docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
-
除了之前的评论:1) 将数组创建移出 for 循环。 2) 为什么不用
ArrayList而不是数组?