Home > OS >  Convert 3D array to 2D array in Java
Convert 3D array to 2D array in Java

Time:10-22

I have a 3D int array named "array[9][3][3]" and I want to convert it into a 2D array "arrayConvt[9][9]". Do not imagine "array[9][3][3]" as a 3D object, but rather a 2D object that has 9 rows and just 1 column and each cell is further divided into a 3X3 2D array. What I want to do is get the content of the first 3 cells and add them side by side to make a 2X8 2D array. Then the next 3 cells (4-6) and add them 1 line below in 2D array to fill 5X8 contents. Then the last 3 cells from the 3D array to the last rows of the 2D array (8X8 cells of the 2D array). The crux of what I have been describing.

CodePudding user response:

Try this.

public static void main(String[] args) {
    int[][][] array = {
        {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}},
        {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}},
        {{20, 21, 22}, {23, 24, 25}, {26, 27, 28}},
        {{30, 31, 32}, {33, 34, 35}, {36, 37, 38}},
        {{40, 41, 42}, {43, 44, 45}, {46, 47, 48}},
        {{50, 51, 52}, {53, 54, 55}, {56, 57, 58}},
        {{60, 61, 62}, {63, 64, 65}, {66, 67, 68}},
        {{70, 71, 72}, {73, 74, 75}, {76, 77, 78}},
        {{80, 81, 82}, {83, 84, 85}, {86, 87, 88}},
    };
    int[][] arrayConv = new int[9][9];

    int[][] s = {
        {0, 0}, {0, 3}, {0, 6},
        {3, 0}, {3, 3}, {3, 6},
        {6, 0}, {6, 3}, {6, 6},
    };

    for (int i = 0, p = 0; i < 9;   i,   p)
        for (int j = 0, r = s[p][0]; j < 3;   j,   r)
            for (int k = 0, c = s[p][1]; k < 3;   k,   c)
                arrayConv[r][c] = array[i][j][k];

    for (int[] r : arrayConv)
        System.out.println(Arrays.toString(r));
}

output:

[0, 1, 2, 10, 11, 12, 20, 21, 22]
[3, 4, 5, 13, 14, 15, 23, 24, 25]
[6, 7, 8, 16, 17, 18, 26, 27, 28]
[30, 31, 32, 40, 41, 42, 50, 51, 52]
[33, 34, 35, 43, 44, 45, 53, 54, 55]
[36, 37, 38, 46, 47, 48, 56, 57, 58]
[60, 61, 62, 70, 71, 72, 80, 81, 82]
[63, 64, 65, 73, 74, 75, 83, 84, 85]
[66, 67, 68, 76, 77, 78, 86, 87, 88]

CodePudding user response:

First, construct some source data

int src[][][] = new int[9][3][3];
for (int r = 0; r < 9; r  ) {
    for (int rr = 0; rr < 3; rr  ) {
        for (int cc = 0; cc < 3; cc  ) {
            src[r][rr][cc] = r * 9   rr * 3   cc   1;
        }
    }
}
for (int[][] d : src) {
    System.out.println(Arrays.deepToString(d));
}

prints

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[10, 11, 12], [13, 14, 15], [16, 17, 18]]
[[19, 20, 21], [22, 23, 24], [25, 26, 27]]
[[28, 29, 30], [31, 32, 33], [34, 35, 36]]
[[37, 38, 39], [40, 41, 42], [43, 44, 45]]
[[46, 47, 48], [49, 50, 51], [52, 53, 54]]
[[55, 56, 57], [58, 59, 60], [61, 62, 63]]
[[64, 65, 66], [67, 68, 69], [70, 71, 72]]
[[73, 74, 75], [76, 77, 78], [79, 80, 81]]
    

Now transform the matrix

int[][] dst = transform(src);

for (int[] row : dst) {
    System.out.println(Arrays.toString(row));
}

prints

[1, 2, 3, 10, 11, 12, 19, 20, 21]
[4, 5, 6, 13, 14, 15, 22, 23, 24]
[7, 8, 9, 16, 17, 18, 25, 26, 27]
[28, 29, 30, 37, 38, 39, 46, 47, 48]
[31, 32, 33, 40, 41, 42, 49, 50, 51]
[34, 35, 36, 43, 44, 45, 52, 53, 54]
[55, 56, 57, 64, 65, 66, 73, 74, 75]
[58, 59, 60, 67, 68, 69, 76, 77, 78]
[61, 62, 63, 70, 71, 72, 79, 80, 81]
  • sr,sc - the source row and column of the inner 3x3 matrix to copy
  • r - the destination row to contain the above
  • the outer loop is used to create the destination row as well as allow
    intermediate increments of key variables
  • and the JavaDoc explains System.arraycopy
public static int[][] transform(int[][][] src) {
  int sr = 0;
  int[][] dst = new int[9][9];
  for (int x = 0; x < 3; x  ) {
     for (int sc = 0; sc < 3; sc  ) {
        int r = x * 3   sc;
        System.arraycopy(src[sr][sc], 0, dst[r], 0, 3);
        System.arraycopy(src[sr   1][sc], 0, dst[r], 3, 3);
        System.arraycopy(src[sr   2][sc], 0, dst[r], 6, 3);
     }
     sr = sr   3;
  }
  return dst;
}
  • Related