将2D阵列填充到Java中的3D阵列

发布于 2025-02-03 07:29:46 字数 1119 浏览 2 评论 0原文

我想编写一种方法,其中我获得了一个3x6工作场(但也可能是3x5、3x4 ...)和一个3x3(也许也可能是不同的尺寸)过滤器,然后制作一个3D阵列(HoldArrays),我将第一个数组放在第一个数组中制作一个正方形(在这种情况下为3x3),然后移动一个位置并将其放入另外3x3,直到覆盖了WorkingArray的最后一列。

不知何故,我在“ //受影响的行”中获得了NullPoInterException,我看不出原因。因此,我想知道我是否以正确的方式进行操作,并且还没有找到如何将2D阵列放入3D数组的资源。

例子:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗼ふ静 2025-02-10 07:29:46

未指定最后一个维度的大小。而且由于行
HoldArrays [i] [J] [K]试图访问不存在的KTH元素,它会引发NULL指针异常。

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

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][];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文