从二维数组中删除边
我有一个数组 [300][300],我需要去掉边缘并用剩余的值填充一个新数组 [298][298]。到目前为止我已经做到了:
double[][] edging() {
double [][] array = new double [data.length] [data[0].length];
array = bubbles();
double [] [] newArray = new double[array.length-2][array[0].length-2];
System.arraycopy(array,1,newArray, 0, newArray.length);
array = newArray;
System.out.println( + newArray.length);
System.out.println( + newArray[0].length);
return newArray;
}
这可以去除顶部和底部边缘。然而,左右依然存在。那里的检查显示新数组是 [298][300]。
我考虑过编写一个循环来依次删除每一行,对每一行使用 arraycopy 并返回新行。但不确定这是否非常有效。
尝试过这个但没有效果。下一个测试是旋转它并再次使用上面的代码。
I have an array [300][300] and I need to get rid of the edges and populate a new array [298][298] with the remaining values. I've got this far so far:
double[][] edging() {
double [][] array = new double [data.length] [data[0].length];
array = bubbles();
double [] [] newArray = new double[array.length-2][array[0].length-2];
System.arraycopy(array,1,newArray, 0, newArray.length);
array = newArray;
System.out.println( + newArray.length);
System.out.println( + newArray[0].length);
return newArray;
}
This works to remove the top and bottom edges. However, the right and left are still there. The checks in there show me that the new array is [298][300].
I've thought about writing a loop to remove each row in turn, use arraycopy on each one and return the new row. Not sure if this would be very efficient though.
Tried this but to no avail. Next one up for a test is rotating it and using the above code again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
干得好。您不一定要使用 System.arraycopy(),因为它仍然在 O(n^2) 运行时间内执行。在下面的程序中,我没有处理边缘情况,但这应该可以为您解决。
上述程序的输出是
Here you go. You do not necessarily want to use the System.arraycopy() since it still performs in O(n^2) running time. In the below program, I have not handled the edge cases, but this should do it for you.
The output of the above program is