Home > Enterprise >  Filling in 2D Array into 3D Array in Java
Filling in 2D Array into 3D Array in Java

Time:06-01

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
                }
            }
        }
    }

CodePudding user response:

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][];
  • Related