我正在使用 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("=======================================================================");
}
}
}
}
出现以下错误:-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at array2d.TugasArray3.main(TugasArray3.java:27) Score Mid Test1 Ke -1
最佳答案
声明一个三维 (3d) 数组 penilaian = new int[i][mahasiswa.length][3];
当 i=0
的大小第一个元素是 0
,因此您无法为此数组分配任何值
关于java - 索引 0 超出长度 0 数组 3D 的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58888232/