Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 如何正确地将变量添加到数组中?_Java_Arrays_Debugging_Error Handling - Fatal编程技术网

Java 如何正确地将变量添加到数组中?

Java 如何正确地将变量添加到数组中?,java,arrays,debugging,error-handling,Java,Arrays,Debugging,Error Handling,我试图给数组分配一个变量,但我一直在得到ArrayIndexOutOfBounds错误,我很难理解为什么它不起作用 问题是: 编写一个程序,读取0到50(含0到50)范围内的任意数量的整数,并统计每个整数的出现次数。通过超出范围的值指示输入的结束。处理完所有输入后,打印一次或多次输入的所有值(带有出现次数) import java.util.Scanner; public class IntCounter { public static void main (String[]args) { /

我试图给数组分配一个变量,但我一直在得到ArrayIndexOutOfBounds错误,我很难理解为什么它不起作用

问题是: 编写一个程序,读取0到50(含0到50)范围内的任意数量的整数,并统计每个整数的出现次数。通过超出范围的值指示输入的结束。处理完所有输入后,打印一次或多次输入的所有值(带有出现次数)

import java.util.Scanner;
public class IntCounter {
public static void main (String[]args) {

//variables 
int i = 0;
final int MaxValue = 51;

int userInput[]=new int[i]; //Initializing array to store user unput
Scanner scan = new Scanner(System.in); //Initializing scanner




   for(i=0; i<MaxValue; i++) {

        System.out.println("please enter a number" + "between 0 and      50, or greater then 50 to finish"");
        int u = scan.nextInt();

        if (u<MaxValue) {
        userInput[i]=u; //THIS IS WHERE THE ERROR IS
        }

    }



 //outputs array values after typing a value out of range
    for(int o=0; o<=i; o++,i++) {
        System.out.println("Your values are:" + userInput[i]);


    }
在这里,您将
userInput
初始化为
0
,也许您的意思是:

int userInput[]=new int[MaxValue];
这将声明数组的大小为
51

ArrayIndexOutofBounds每当您尝试访问数组中大于数组大小的位置时,都会发生异常


显然,
0
的大小将不能包含任何元素,因此总是抛出
异常

您已声明大小i的数组,如下所示

int userInput[]=new int[i]
但是,的值为0
因此,它正在创建大小为0的数组,从而导致ArrayIndexOutOfBoundsException

数组长度在初始化后是不可变的。您正在使用值为0的I初始化数组,因此数组的长度为0。使用int-userInput[]=new-int[MaxValue]初始化它您所问的问题的答案是,您正在创建一个大小为0的数组,然后尝试将内容放入其中(int-userInput[]=new-int[i],其中“i”是0)。显然,你不能这样做,因此出现了错误。也就是说,你似乎对这个问题的目的有误解。我假设这是一个家庭作业问题,所以我不会直接给你答案。我可以肯定地说,您不需要存储每个输入整数。想一想不这样做如何解决这个问题。在这种情况下,任意数量的输入实际上与数组的设置方式无关(他不需要
ArrayList
)。每次数组中出现该数字时,他所需要做的就是添加
1
。例如,如果键入
34
,则数组中的位置34现在为
1
。再次键入,现在是
2
。然后在循环之后,您可以打印1或以上的所有位置。这仍然是使用数组时输入的任意长度。
int i = 0;
final int MaxValue = 51;

int userInput[]=new int[i];
int userInput[]=new int[MaxValue];
int userInput[]=new int[i]