简体   繁体   English

将2D Array填入3D Array in Java

[英]Filling in 2D Array into 3D Array in Java

I want to write a Method where I get a 3x6 workingArray (but might also be 3x5, 3x4...) and a 3x3 (maybe also different size) filteringArray and make an 3D Array (holdArrays) where I put in the first Array to make a square (in this case 3x3), then move one position and put in another 3x3, till the last column of workingArray has been covered.我想写一个方法,我得到一个 3x6 workingArray(但也可能是 3x5、3x4...)和一个 3x3(也可能大小不同)filteringArray 并制作一个 3D 数组(holdArrays),我将第一个数组放入制作一个正方形(在本例中为 3x3),然后移动一个 position 并放入另一个 3x3,直到覆盖 workingArray 的最后一列。

Somehow I get an nullPointerException in '//affected line' and I can't see why.不知何故,我在“//受影响的行”中得到了一个 nullPointerException,但我不明白为什么。 So I wonder if I do this the right way and I haven't found resources for how to put 2D Arrays into 3D Array.所以我想知道我是否以正确的方式执行此操作,而且我还没有找到有关如何将 2D Arrays 放入 3D 数组的资源。

Example:例子:

1 2 3 4      1 2 3       2 3 4
5 6 7 8  to  5 6 7  and  6 7 8
9 1 2 3      9 1 2       1 2 3
private static void filtering(double[][] workingArray, double[][] filteringArray) {
    
    // creating amount of holdArrays to work with filterArray
    double[][][] holdArrays = new double[workingArray.length - filteringArray.length + 1][filteringArray.length][];

        // filling in the parted workingArray into holdArrays
        for (int i = 0; i < holdArrays.length; i++) {

            for (int j = 0; j < filteringArray.length; j++) {

                for (int k = 0; k < filteringArray[j].length; k++) {

                    holdArrays[i][j][k] = workingArray[j][k]; //affected line
                }
            }
        }
    }

The size of the last dimension is not specified.未指定最后一个维度的大小。 And since the line并且由于行
holdArrays[i][j][k] tries to access the kth element which is not there, it throws a Null Pointer Exception. holdArrays[i][j][k]尝试访问不存在的第 k 个元素,它会抛出 Null 指针异常。

 double[][][] holdArrays = new double[workingArray.length - filteringArray.length + 1][filteringArray.length][];

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

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