Possible Duplicate:
java: How to split a 2d array into two 2d arrays
所需的是拆分此数组:
int[][] bitblock = {
{1,0,1,0,1,0,1,0},
{1,0,1,0,1,0,1,0},
{1,0,1,0,1,0,1,0},
{1,0,1,0,1,0,1,0},
{1,0,1,0,1,0,1,0},
{1,0,1,0,1,0,1,0},
{1,0,1,0,1,0,1,0},
{1,0,1,0,1,0,1,0}};
这是一个8 * 8数组,我想将其拆分为左右数组并将其存储在此处:
int[][] leftblock = new int [bitblock.length][bitblock[0].length/2];
int[][] rightblock = new int [bitblock.length][bitblock[0].length/2];
我使用了Syste.arraycopy方法,并且能够将位块拆分为上下数组,我在努力将其拆分为左右:
System.arraycopy(bitblock, 0, leftblock, 0, leftblock.length);
System.arraycopy(bitblock, rightblock.length, rightblock, 0, rightblock.length)
有人可以帮忙吗?谢谢
最佳答案
这应该很好地完成了这个技巧: int[][] bitblock = {{1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 0}};
ojit_pre
您还可以使用System.arraycopy,因为它使代码更清晰以备将来检查。
至于性能,只要阵列那么小,我认为这并不重要。