简体   繁体   中英

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.

Somehow I get an nullPointerException in '//affected line' and I can't see why. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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