简体   繁体   English

为什么会收到“ java.lang.ArrayIndexOutOfBoundsException:0”

[英]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();
    }



        }
    }








    }


}

For your case 对于你的情况

int [][] matrix = new int [0][0];

Means with no element , index starts from 0, where the size you specify starts from 1 没有元素的均值,索引从0开始,其中您指定的大小从1开始

Go for ArrayList if you want dynamic array 如果要动态数组,请选择ArrayList

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. 当然,您可以只使用rowcolumn变量,而不是从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. rowcolum设置为> 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: 您的问题是将rowcolumn设置为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.

相关问题 为什么我在这里得到 java.lang.ArrayIndexOutOfBoundsException ? - Why do I get java.lang.ArrayIndexOutOfBoundsException here? 如何摆脱此java.lang.ArrayIndexOutOfBoundsException错误? - How do i get rid of this java.lang.ArrayIndexOutOfBoundsException error? 为什么在线程“ AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException:2中得到异常? - Why do I get Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 2? 为什么会出现java.lang.ArrayIndexOutOfBoundsException异常? - Why am I getting java.lang.ArrayIndexOutOfBoundsException exception ? 为什么我得到java.lang.ArrayIndexOutOfBoundsException - Why am I getting java.lang.ArrayIndexOutOfBoundsException 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 异常,我该如何预防? - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? java.lang.ArrayIndexOutOfBoundsException:0 - java.lang.ArrayIndexOutOfBoundsException:0 java.lang.ArrayIndexOutOfBoundsException:2&gt; = 2 - java.lang.ArrayIndexOutOfBoundsException: 2 >= 2 java.lang.ArrayIndexOutOfBoundsException:2&gt; = 0 - java.lang.ArrayIndexOutOfBoundsException: 2 >= 0 java.lang.ArrayIndexOutOfBoundsException - java.lang.ArrayIndexOutOfBoundsException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM