繁体   English   中英

索引 0 超出长度 0 数组 3D 的范围

[英]Index 0 out of bounds for length 0 Array 3D

我正在使用 3D 数组 int 下面的代码我得到数组索引超出范围错误下面是相同的代码:-

package array2d;

import java.util.Scanner;

public class Array3 {

    public static void main(String[] Args) {
        Scanner vee= new Scanner(System.in);
        int penilaian[][][];
        int mahasiswa[];

        for (int i = 0; i < 5; i++) {
            System.out.print("Class" + (i + 1) + " : ");
            int mhs = vee.nextInt();

            System.out.println("------------------------------------------------");
            mahasiswa = new int[mhs];

            penilaian = new int[i][mahasiswa.length][3];

            for (int j = 0; j < mahasiswa.length; j++) {
                System.out.print("Score Mid Test" + (i + 1) + " Ke -" + (j + 1) + " :");
                penilaian[i][j][0] = vee.nextInt();
                System.out.print("Score Test" + (i + 1) + " Ke -" + (j + 1) + " :");
                penilaian[i][j][1] = vee.nextInt();
                System.out.print("Score Project" + (i + 1) + " Ke -" + (j + 1) + " :");
                penilaian[i][j][2] = vee.nextInt();
                System.out.println("=======================================================================");

            }
        }
    }

}

收到以下错误:-

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:索引 0 在 array2d.TugasArray3.main(TugasArray3.java:27) 中的长度 0 超出范围 -1

您声明一个三维 (3d) 数组penilaian = new int[i][mahasiswa.length][3]; i=0时,第一个元素的大小为0 ,因此您不能为该数组分配任何值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM