我在JAVA中編寫了以下代碼。在JAVA中打印3D數組時發生ArrayIndexOutOfBoundsException
package threed;
import java.util.Scanner;
public class Threed_Array {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int b1,b2,b3;
System.out.print("Enter the number of elements in 1st bracket->");
Scanner sc=new Scanner(System.in);
b1=sc.nextInt();
System.out.print("Enter the number of elements in 2nd bracket->");
b2=sc.nextInt();
System.out.print("Enter the number of elements in 3rd bracket->");
b3=sc.nextInt();
int threedarray[][][]=new int[b1][b2][b3];
for(int i=1; i<=b1; i++)
{
for(int j=1; i<=b2; j++)
{
for(int k=1; i<=b3; k++)
{
System.out.print("Enter element->");
threedarray[i][j][k]=sc.nextInt();
}
}
}
for(int i=1; i<=b1; i++)
{
for(int j=1; i<=b2; j++)
{
for(int k=1; i<=b3; k++)
{
System.out.print(" "+threedarray[i][j][k]);
}
}
}
}
}
我得到這個代碼的ArrayIndexOutOfBoundsException。這是顯示在行中:
threedarray[i][j][k]=sc.nextInt();
任何人都可以幫助我出錯的地方?謝謝。
在Java中,數組索引從0開始到(n-1),其中'n'是數組的長度。 –