簡體   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