我正在做一个项目,我必须从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;
}
我尝试过这段代码,它可以重塑数组的形状,但只显示零的二维数组。
答案 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;
}
逻辑:
trans
和r
和c
处创建所需尺寸的二维数组。ma
的所有元素收集到列表collectList
答案 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]]
---------------------------