将2D阵列填充到Java中的3D阵列
我想编写一种方法,其中我获得了一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未指定最后一个维度的大小。而且由于行
HoldArrays [i] [J] [K]
试图访问不存在的KTH元素,它会引发NULL指针异常。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.