Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.ArrayIndexOutOfBoundsException当我尝试向数组添加变量时_Java_Arrays_Syntax - Fatal编程技术网

java.lang.ArrayIndexOutOfBoundsException当我尝试向数组添加变量时

java.lang.ArrayIndexOutOfBoundsException当我尝试向数组添加变量时,java,arrays,syntax,Java,Arrays,Syntax,我试图通过扫描仪输入将数字保存到数组中,但 “java.lang.ArrayIndexOutOfBoundsException”不断出现 这是我的密码: for (int counter = 1;1==1;counter++) { int bucky[] = new int[counter]; int scan = oof.nextInt(); bucky[counter] = scan; if (c

我试图通过扫描仪输入将数字保存到数组中,但
“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);
            }
有人能解释一下我做错了什么,或者我在思考过程中哪里做错了吗?

试试看

for (int counter = 1;1==1;counter++) {
            int bucky[] = new int[counter];
            int scan = oof.nextInt();
            bucky[counter-1] = scan;
            if (counter == 5) {
                System.out.println(bucky);
            }
5个位置的一个数组{0,1,2,3,4}

您的数组有1个位置{0}

bucky[计数器-1]=扫描


数组的第一个索引是什么?假设它的长度为1,它的最后一个索引是什么?除了前面的注释:1)将数组创建移出for循环。2) 为什么不使用
ArrayList
而不是array?我得到的是werid输出[I@16d3586is这是编译器中的一个小故障,或者something@DudeManGuy@DudeManGuy简便的经验法则:编译器(;->)从来都不是小故障