在Java中,如何获取2D数组的值并将它们存储在具有不同列和行大小的另一个2D数组中?

时间:2019-02-17 06:29:16

标签: java arrays matrix multidimensional-array

我正在做一个项目,我必须从2D阵列制作一个矩阵。要求之一是将3x4 2D数组(具有存储的值)转换为6x2 2d数组(具有相同的值)?

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;
}

我尝试过这段代码,它可以重塑数组的形状,但只显示零的二维数组。

3 个答案:

答案 0 :(得分:1)

您可以将方法更改为:

public int[][] covertMatrix(int[][] ma, int r, int c) {
   int trans[][] = new int[r][c]; 
   int count = 0;  // used to increment the list elements

   // fetch all elements from the original array 'ma'
   List<Integer> collectList = Arrays.stream(ma).flatMapToInt(Arrays::stream)
                                     .boxed().collect(Collectors.toList());

   // assign the values from the list to resp array indices
   for (int i = 0; i < r; i++) {
       for (int j = 0; j < c; j++) {
           trans[i][j] = collectList.get(count);
           count++;
       }
   }

   return trans;
}

逻辑:

  1. transrc处创建所需尺寸的二维数组。
  2. 现在将数组ma的所有元素收集到列表collectList
  3. 遍历新创建的数组,从列表中获取值,并将其分配给相应的索引。

答案 1 :(得分:1)

此版本将值重新分配给新数组。

通过将单元格索引除以给出行的列,并计算其余的给新列来计算索引。

public int[][] covertMatrix(int[][] ma, int r, int c) {
    rw = r;
    col = c;
    int element = 0;
    int[][] ma2 = new int[rw][col];
    for (int i = 0; i < ma.length; i++) {
        for (int j = 0; j < ma[i].length; j++) {
           final int newRow = (element)/col; //integer division ignoring rest.
           final int newCol = (element)%col; // rest of the division.
           ma2[newRow][newCol] = ma[i][j];
           element++;
        }
    }
    this.ma = ma2;
    return ma2;
}



System.err.println(Arrays.deepToString(covertMatrix(new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12}},6,2)));
-> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]

答案 2 :(得分:0)

您拥有的是3x4的起始矩阵,例如

+--+--+--+--+
|01|02|03|04|
+--+--+--+--+
|05|06|07|08|
+--+--+--+--+
|09|10|11|12|
+--+--+--+--+
您要在6x2矩阵中转换的

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

为此,显然tab1[i][j] = tab2[i][j]将不起作用。您需要在两个数组之间转换地址。乍一看,对行使用模,对col使用除法的其余部分将达到目的。

类似

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("---------------------------");
    }

给出输出内容

[[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]]
---------------------------