In Java, how do I take the values of a 2D array and store them in another 2D array with different column and row size?

klion :

I am doing a project where I have to make a matrices from a 2D array. One of the requirements is to covert a 3x4 2D array (with values stored in) to a 6x2 2d array (with the same values)?

public int[][] covertMatrix(int[][] ma, int r, int c) {
        rw = r;
        col = c;

        this.ma = new int[rw][col];
        for (int i = 0; i < rw; i++) {
            for (int j = 0; j < col; j++) {
                ma[i][j] = ma[i][j];    
            }
        }
        return ma;
}

I've tried this code and it reshapes the array but only prints a 2D array of zeroes.

Wisthler :

What you have is a starting matrix of 3x4 like

+--+--+--+--+
|01|02|03|04|
+--+--+--+--+
|05|06|07|08|
+--+--+--+--+
|09|10|11|12|
+--+--+--+--+

that you want to convert in a 6x2 matrix like

+--+--+
|01|02|
+--+--+
|03|04|
+--+--+
|05|06|
+--+--+
|07|08|
+--+--+
|09|10|
+--+--+
|11|12|
+--+--+

To do that, clearly tab1[i][j] = tab2[i][j] will not work. You need to translate address between the two arrays. At first sight, using the modulo for the row and the rest of the division for the col would do the trick.

Something like

public static void main(String[] args) {
        int[][] tab1 = new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        System.out.println(Arrays.deepToString(tab1));
        System.out.println("---------------------------");
        int size = tab1.length * tab1[0].length;
        for(int i = 1; i <= size; i++){
            int j = size % i;
            if(j == 0){
                convert(tab1, i, size/i);
            }
        }

    }

    private static void convert(int[][] tab1, int row, int col) {
        System.out.println(String.format("converting to %dx%d", row, col));
        int[][] tab2 = new int[row][col];
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                int index = i*col + j;
                int newRow = index / tab1[0].length;
                int newCol = index % tab1[0].length;
                tab2[i][j] = tab1[newRow][newCol];
            }
        }
        System.out.println(Arrays.deepToString(tab2));
        System.out.println("---------------------------");
    }

Which give the output

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
---------------------------
converting to 1x12
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
---------------------------
converting to 2x6
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
---------------------------
converting to 3x4
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
---------------------------
converting to 4x3
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
---------------------------
converting to 6x2
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
---------------------------
converting to 12x1
[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]]
---------------------------

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=82178&siteId=1