我在下面的代码中使用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超出长度0 在array2d.TugasArray3.main(TugasArray3.java:27) 得分中考1 Ke -1
答案 0 :(得分:0)
当第一个元素的大小为penilaian = new int[i][mahasiswa.length][3];
时,如果您不能为该数组分配任何值,则声明一个三维(3d)数组i=0