Possible Duplicate:
java: How to split a 2d array into two 2d arrays
What is required is to split this array:
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}};
This is an 8*8 array , I want to split it into left and right arrays and store them here:
int[][] leftblock = new int [bitblock.length][bitblock[0].length/2];
int[][] rightblock = new int [bitblock.length][bitblock[0].length/2];
I used the method Syste.arraycopy and I was able to split the bitblock to up and down arrays, I am kind of struggling to split it left and right:
System.arraycopy(bitblock, 0, leftblock, 0, leftblock.length);
System.arraycopy(bitblock, rightblock.length, rightblock, 0, rightblock.length)
Can someone please help ? Thanks
This should do the trick nicely:
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}};
You could also use System.arraycopy, as it makes the code more clear for future inspection.
As of performance, I don’t think it matters as long as your array is that small.