I have a rank 3 int array array[9][3][3]
and I want to convert it into a rank 2 array arrayConvt[9][9]
by removing the major axis (rather than the middle axis). To make a 9x9 array rather than 3x27, imagine array
broken up into 3 equal parts, each laid out into arrayConvt
before the next. Note that the middle arrays (array[i]
) do not remain contiguous in arrayConvt
, but the innermost arrays (array[i][j]
) do.
One way to visualize it is to look at array
as an array of 9 blocks. I want to recombine the blocks left-to-right, top-to-bottom:
How can I reshape array
according to this mapping?
The code sample below provides data to work with and the desired result:
public static void main(String[] args) {
int[][][] array = {
{
{0, 1, 2},
{10, 11, 12},
{20, 21, 22}
},
{
{100, 101, 102},
{110, 111, 112},
{120, 121, 122}
},
{
{200, 201, 202},
{210, 211, 212},
{220, 221, 222}
},
{
{300, 301, 302},
{310, 311, 312},
{320, 321, 322}
},
{
{400, 401, 402},
{410, 411, 412},
{420, 421, 422}
},
{
{500, 501, 502},
{510, 511, 512},
{520, 521, 522}
},
{
{600, 601, 602},
{610, 611, 612},
{620, 621, 622}
},
{
{700, 701, 702},
{710, 711, 712},
{720, 721, 722}
},
{
{800, 801, 802},
{810, 811, 812},
{820, 821, 822}
}
};
int[][] arrayConvt;
/*****
* What should go here to fill out `arrayConvt` using entries from `array` so it's equivalent to `array2d` below?
*/
int[][] array2d = {
{ 0, 1 , 2, 100, 101, 102, 200, 201, 202},
{ 10, 11, 12, 110, 111, 112, 210, 211, 212},
{ 20, 21, 22, 120, 121, 122, 220, 221, 222},
{300, 301, 302, 400, 401, 402, 500, 501, 502},
{310, 311, 312, 410, 411, 412, 510, 511, 512},
{320, 321, 322, 420, 421, 422, 520, 521, 522},
{600, 601, 602, 700, 701, 702, 800, 801, 802},
{610, 611, 612, 710, 711, 712, 810, 811, 812},
{620, 621, 622, 720, 721, 722, 820, 821, 822}
};
}