java - 3维数组索引越界

标签 java arrays indexoutofboundsexception multidimensional-array

这是我的第一个问题。我正在尝试编写一个程序,该程序读取与从键盘输入一样多的数组。输入列数后出现错误: Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException: 0

提前致谢。

package Final;

import java.util.*;

public class Final {
    int a,b,x,l;
    double mat[][][];

    Final()
    {
        a=0;
        b=0;
        c=0;
        d=0;
        x=0;
        l=0;
    }

    public void read() {
        Scanner citast = new Scanner(System.in);
        System.out.println("How many arrays do you want to enter?");
        x=citast.nextInt();
        for (this.l=0; this.l<x; this.l++)
        {
              System.out.println("Matrix "+this.l);
              System.out.println("Number of lines: ");
              this.a = citast.nextInt();
              System.out.println("Number of columns: ");
              this.b = citast.nextInt();
              this.mat= new double [this.l][a][b];
              for (int i=0; i<this.a; i++)
               {
                  for (int j=0; j<this.b; j++)
                   {
                      System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
                      this.mat[l][i][j]=citast.nextInt();
                   }
                  System.out.println();
            }
        }
    }

    public static void main(String[] args) {
        Final a = new Final();
        Scanner citast = new Scanner(System.in);
        a.read();
        System.out.println();

}

}

最佳答案

this.mat= new double [this.l][a][b];

应该是:

this.mat= new double [x][a][b];

但是你的代码是错误的,你应该在第一个 for 循环之前设置数组的维度:

public void read() {
        Scanner citast = new Scanner(System.in);
        System.out.println("How many arrays do you want to enter?");
        x=citast.nextInt();
        this.mat = new double[x][][]; //<-- added this line
        for (this.l=0; this.l<x; this.l++)
        {
            System.out.println("Matrix "+this.l);
            System.out.println("Number of lines: ");
            this.a = citast.nextInt();
            System.out.println("Number of columns: ");
            this.b = citast.nextInt();
            this.mat[l] =  new double [a][b]; //<-- modified this line
            for (int i=0; i<this.a; i++)
                for (int j=0; j<this.b; j++)
                {
                    System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
                    this.mat[l][i][j]=citast.nextInt();
                }
            System.out.println();
        }
        System.out.println(Arrays.deepToString(mat));
    }

包含 2 个方阵的数组的输出(一个用 4 填充,一个用 5 填充):

[[[4.0, 4.0], [4.0, 4.0]], [[5.0, 5.0], [5.0, 5.0]]]

关于java - 3维数组索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20176028/

相关文章:

javascript - 转换分隔字符串和对象数组

java - 找不到合适的排序方法,推断数组变量具有不兼容的边界

java - 使用 SQL 开发人员从索引中提取字符串

java - Android Realm Java 0.82.1 ArrayIndexOutOfBoundsException

java - 错误的越界异常

java - Jersey Grizzly HTTP 服务器在容器 docker 内关闭

java - Android、Eclipse : Item in . xml 布局未在图形布局中显示

javascript - 使用 array.includes 进行松散相等比较

java - Do-While 循环填充数组

java - 如何用递归编写逆线程序?