java - 将 1D 转换为 2D java.lang.ArrayIndexOutOfBoundsException

标签 java multidimensional-array

我尝试将一维数组转换为二维数组,但我不断收到java.lang.ArrayIndexOutOfBoundsException,并且我尝试了在stackoverflow或互联网上可以找到的任何内容,但我不明白为什么我有这个问题吗?

public class Arrayto2DArray {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int[] a = {0,1, 6, 83, 4, 5, 12, 7};
        int[][] b = new int[4][4];

          for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++) {
                 b[i][j]=0;
                 System.out.print(b[i][j]);
            }
            System.out.println();
          }   

        System.out.println("--------------------------");


        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < b[i].length; j++) {
                try{
                b[i][j] = a[i+j*4];
                }catch(Exception e){
                    e.printStackTrace();
                    System.out.println(e);
                }
            }
        }

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(b[i][j]);
            }
                System.out.println();
        }    
    }
}

我大概知道为什么会出现此错误,并且是因为这一行

 b[i][j] = a[i+j*4];

但我想不出比这更好的公式了。

最佳答案

考虑第二个 for 循环 假设当 i = 3 且 j= 3 时 a[i+j*4]计算结果为 a[15]不在数组中

当您声明二维数组时,您指定了 int[][] b = new int[4][]; ,这意味着第一个内部 for 循环 for (int j = 0; j < b[i].length; j++)b[i].length 起应该会导致 NullPointerException没有预定义的长度。在插入内部 for 循环之前,您应该定义每个 b[i] 的大小,如 b[i] = new int[somenumber]

关于将一维循环转换为二维,您需要定义将其拆分为二维数组的规则。那么相应地第二个for循环需要修改

编辑:您修改了代码以拥有 int[4][4] 数组,这意味着您有 16 个占位符。您的一维数组仅包含 8 个占位符。这取决于您想要如何对数组进行排序,就像它一样

b  0   1   2   3
0  0   1   6   83
1  4   5   12  7
2  0   0   0   0
3  0   0   0   0

或任何其他模式

假设一维数组的长度为8,二维数组的总索引为16,以下是更通用的解决方案:

public static void main(String[] args) {
        // TODO code application logic here
        int[] a = { 0, 1, 6, 83, 4, 5, 12, 7 };
        int[][] b = new int[4][4];

        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++) {
                b[i][j] = 0;
                System.out.print(b[i][j] + "\t");
            }
            System.out.println();
        }

        System.out.println("--------------------------");

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < b[i].length; j++) {
                try {
                    if ((j + i * 4) < a.length)
                        b[i][j] = a[j + i * 4];
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(e);
                }
            }
        }

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(b[i][j] + "\t");
            }
            System.out.println();
        }
    }

关于java - 将 1D 转换为 2D java.lang.ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23773324/

相关文章:

c# - 如何从多维数组中获取维度(切片)

Java "resource"-文件夹有时会自行更改为常用文件夹?

java - Java 可以用来确定下载的持续时间吗?

java - 是否可以使用 Activejdbc 和 Maven 进行仪器模块项目?

java - 检查数组是否已排序

php - 如何去除多维数组中重复的键值数组

Cuda 用 2D block 替换 double

java - 找不到包 java.nio.file

javascript - 到达数组末尾后如何执行函数?

php - 根据两列值和每组中一列的总和对多维数组数据进行分组