[英]Why do I get an “java.lang.ArrayIndexOutOfBoundsException: 0”
What is wrong here? 怎么了 I get an runtime error when I run the code in Netbeans saying "java.lang.ArrayIndexOutOfBoundsException: 0" 当我在Netbeans中运行代码为“ java.lang.ArrayIndexOutOfBoundsException:0”时,出现运行时错误
I found out it means something like "the value does not exist" - but that is what I am trying to do - to let the user define the values for the array size. 我发现这意味着“值不存在”之类的东西-但这就是我要尝试的操作-让用户定义数组大小的值。 Help, please. 请帮助。
public static void main(String[] args) {
int row = 0;
int colum = 0;
//Declare 2d array
int [][] matrix = new int [row][colum];
//Create input for array size
Scanner input = new Scanner(System.in);
System.out.println("Enter " + matrix[row].length + " rows and " + matrix[colum].length + " colums: ");
for (row = 0; row < matrix.length; row++) {
for (colum = 0; colum < matrix[row].length ; colum++) {
matrix[row][colum] = input.nextInt();
//Input variables to array
//Print the array
for (row = 0; row < matrix.length; row++ ) {
for (colum = 0; colum < matrix[row].length; colum++ ) {
System.out.println(matrix[row][colum] + "");
}
System.out.println();
}
}
}
}
}
Two problems. 两个问题。 You need to initialize row and column with non-zero values: 您需要使用非零值初始化行和列:
int row = 3;
int colum = 4;
You're not referencing the dimensions of the matrix correctly, you want: 您没有正确地引用矩阵的尺寸,您需要:
System.out.println("Enter " + matrix.length + " rows and "
+ matrix[0].length + " colums: ");
There are a few more bad references further down. 后面还有其他一些不好的参考。 You could of course just use your row
and column
variables instead of getting the lengths from the matrix
though. 当然,您可以只使用row
和column
变量,而不是从matrix
中获取长度。
您的数组有0个元素-您正在尝试访问第一个(索引为0)-超出范围。
You are creating an array of arrays that has size 0 in both dimensions: 您正在创建两个维度均为0的数组的数组:
int row = 0;
int colum = 0;
//Declare 2d array
int [][] matrix = new int [row][colum];
If you try to index this you will get an ArrayIndexOutOfBoundsException
. 如果尝试对此进行索引,则将获得ArrayIndexOutOfBoundsException
。
Set row
and colum
to something > 0. 将row
和colum
设置为> 0。
Arrays in Java have a fixed size. Java中的数组具有固定大小。 Once you have created an array, you can't change the size of it. 创建数组后,就无法更改其大小。 If you need to change the size at runtime, use a collection class (for example an ArrayList
) instead of an array. 如果需要在运行时更改大小,请使用集合类(例如ArrayList
)而不是数组。
int row = 0;
int colum = 0;
int [][] matrix = new int [row][colum];
The declared array has zero rows and zero columns. 声明的数组具有零行零列。 You can't store anything in it. 您不能在其中存储任何内容。
The exception occurs here: 异常发生在这里:
matrix[row].length
with row=0
because it is the number of columns of the first row (arrays are zero based!) which doesn't exist. row=0
因为它不存在第一行的列数(数组从零开始!)。
Your problem is that you're setting row
and column
to 0, then you create a two-dimensional array of size 0 in both directions, and then this: 您的问题是将row
和column
设置为0,然后在两个方向上创建一个大小为0的二维数组,然后执行以下操作:
matrix[row].length
fails because there is no entry at index 0 - it would require the array to have at least size 1. 失败,因为在索引0处没有任何条目-它要求数组的大小至少为1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.