繁体   English   中英

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

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

我正在做一个项目,我必须从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;
}

我已经尝试过这个代码,它重新整形了数组,但只打印了一个零的二维数组。

您可以将方法更改为:

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. 迭代新创建的数组,从列表中获取值并将它们分配给相应的索引。

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

通过将cellindex除以给出行并计算其余部分的colums来计算索引。

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

你有一个像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]]
---------------------------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM