繁体   English   中英

在JAVA中打印3D数组时出现ArrayIndexOutOfBoundsException

[英]ArrayIndexOutOfBoundsException while printing a 3D array in JAVA

我用JAVA编写了以下代码。

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();

有人可以帮我解决错误发生的地方吗? 谢谢。

您应该始终从索引0开始,它是数组第一个元素的索引:

for(int i=0; i<b1; i++)
{
    for(int j=0; j<b2; j++)
    {
        for(int k=0; k<b3; k++) {
          System.out.print("Enter element->");
          threedarray[i][j][k]=sc.nextInt();
        }
    }
}
for(int i=0; i<b1; i++)
{
    for(int j=0; j<b2; j++)
    {
        for(int k=0; k<b3; k++)
        {
          System.out.print(" "+threedarray[i][j][k]);
        }
    }
}

此外,用< not <=

在最后一个循环中,您访问数组元素n + 1,其中n是该数组的大小。 多数民众赞成在例外的原因。

我会说您循环中的条件不正确:

for(int i=1; i<=b1; i++)
{
    for(int j=1; i<=b2; j++)
    {
        for(int k=1; i<=b3; k++)
        {

它应该是 :

for(int i=1; i<=b1; i++)
{
    for(int j=1; j<=b2; j++)
    {
        for(int k=1; k<=b3; k++)
        {

另外,您每个都应从0开始。

我认为您希望jk在2个for循环中而不是i 另外,Java中的数组从索引0开始,因此它应如下所示:

for(int i=0; i<b1; i++)
{
    for(int j=0; j<b2; j++)
    {
        for(int k=0; k<b3; k++)
        {  
           ...
        }
     }
}

Java中的数组从零开始,请尝试从0迭代到b1-1:

for(int i=0; i<b1; i++)
{
    for(int j=0; i<b2; j++)
    {
        for(int k=0; i<b3; k++)
        {
          System.out.print("Enter element->");
          threedarray[i][j][k]=sc.nextInt();
        }
    }
}

[b1][b2][b3]

使用输入b1b2b3创建一个数组

创建的数组的长度为bx但下标从0bx-1 因此,您必须从0循环到bx-1

索引从0开始,而不是1 0处开始三个for循环,并迭代到比提供的次数少的一个:

for(int i = 0; i < b1; i++)
{
    for(int j = 0; i < b2; j++)
    {
        for(int k = 0; i < b3; k++)
        {
          System.out.print(" "+threedarray[i][j][k]);
        }
    }
}

除了数组索引问题(请参阅Stefan Beike的答案 ),您还可以使用ArrayList

这样可以避免向用户询问要在矩阵中包含的元素数量。

仍然,如果要保留基本数组,则可以使用System.arrayCopy重新分配给更大的数组。

这里的问题是您要分别从1到b1,b2,b3开始循环。 数组索引从0开始而不是从1开始,并以数组大小-1结束。因此,要修复代码,您需要将代码修改为以下内容:

for(int i = 0; i < b1; i++) {
  for(int j = 0; j < b2; j++) {
    for(int k = 0; k < b3; k++) {
      System.out.print("Enter element->");
      threedarray[i][j][k]=sc.nextInt();
    }
  }
}

更一般而言,如果您不知道要循环的数组的长度/大小,则可以使循环更一般地小于数组的长度。 因此,将是i <threedarray.length和j <threedarray [i] .length和k <threedarray [i] [j] .length。

关键思想是数组索引从0开始,以其大小/长度-1结束,因此要获取您访问array [array.length-1]的最后一个元素并访问您访问array [0]的第一个元素

希望这能回答您的问题。

暂无
暂无

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

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