Java 有没有公式可以这样重新排列二维数组?
是否有对该数组进行排序的公式Java 有没有公式可以这样重新排列二维数组?,java,arrays,Java,Arrays,是否有对该数组进行排序的公式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 到 取第一列并将其放在二维数组的第一位 这是重新定位后的位置 [0][0] --> [0][0] [1][0] --> [0][1] [2][0] --> [0][2] [3][0] --> [1][0] [4][0] --> [1][1] [5][0] --> [1][2] [6]
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
到
取第一列并将其放在二维数组的第一位
这是重新定位后的位置
[0][0] --> [0][0]
[1][0] --> [0][1]
[2][0] --> [0][2]
[3][0] --> [1][0]
[4][0] --> [1][1]
[5][0] --> [1][2]
[6][0] --> [2][0]
[0][1] --> [2][1]
[1][1] --> [2][2]
[2][1] --> [3][0]
[3][1] --> [3][1]
[4][1] --> [3][2]
[5][1] --> [4][0]
[6][1] --> [4][1]
[0][2] --> [4][2]
[1][2] --> [5][0]
[2][2] --> [5][1]
[3][2] --> [5][2]
[4][2] --> [6][0]
[5][2] --> [6][1]
[6][2] --> [6][2]
我能看到模式,但想不出公式
如果我是对的,我想用这个
for(int i = 0 ; i < 3 ; i++){
for(int j = 0 ; j < 7 ; j++){
//do something here
}
}
for(int i=0;i<3;i++){
对于(int j=0;j<7;j++){
//在这里做点什么
}
}
您可以这样做:
public static int[][] rearrange(int[][] input) {
int rows = input.length, cols = input[0].length, total = rows * cols;
int[][] output = new int[rows][cols];
for (int i = 0; i < total; i++)
output[i / cols][i % cols] = input[i % rows][i / rows];
return output;
}
for (int j = 0, p = 0; j < 7; j++) {
for (int i = 0; i < 3; i++, p++) {
newMatrix[j][i] = oldMatrix[p % 7][p / 7];
}
}
输出
[[1,4,7]、[10,13,16]、[19,2,5]、[8,11,14]、[17,20,3]、[6,9,12]、[15,18,21]]
您可以这样做:
public static int[][] rearrange(int[][] input) {
int rows = input.length, cols = input[0].length, total = rows * cols;
int[][] output = new int[rows][cols];
for (int i = 0; i < total; i++)
output[i / cols][i % cols] = input[i % rows][i / rows];
return output;
}
for (int j = 0, p = 0; j < 7; j++) {
for (int i = 0; i < 3; i++, p++) {
newMatrix[j][i] = oldMatrix[p % 7][p / 7];
}
}
输出
[[1,4,7]、[10,13,16]、[19,2,5]、[8,11,14]、[17,20,3]、[6,9,12]、[15,18,21]]
或类似的内容:
public static int[][] rearrange(int[][] input) {
int rows = input.length, cols = input[0].length, total = rows * cols;
int[][] output = new int[rows][cols];
for (int i = 0; i < total; i++)
output[i / cols][i % cols] = input[i % rows][i / rows];
return output;
}
for (int j = 0, p = 0; j < 7; j++) {
for (int i = 0; i < 3; i++, p++) {
newMatrix[j][i] = oldMatrix[p % 7][p / 7];
}
}
for(int j=0,p=0;j<7;j++){
对于(int i=0;i<3;i++,p++){
新矩阵[j][i]=旧矩阵[p%7][p/7];
}
}
或类似的内容:
public static int[][] rearrange(int[][] input) {
int rows = input.length, cols = input[0].length, total = rows * cols;
int[][] output = new int[rows][cols];
for (int i = 0; i < total; i++)
output[i / cols][i % cols] = input[i % rows][i / rows];
return output;
}
for (int j = 0, p = 0; j < 7; j++) {
for (int i = 0; i < 3; i++, p++) {
newMatrix[j][i] = oldMatrix[p % 7][p / 7];
}
}
for(int j=0,p=0;j<7;j++){
对于(int i=0;i<3;i++,p++){
新矩阵[j][i]=旧矩阵[p%7][p/7];
}
}
这不是“排序”,而是“重新安排”(或其他什么)。当然不是“排序”。如果你能把“数组”放进一个代码语法中,就更容易弄清楚这不是“排序”,而是“重新排列”(或者其他什么)。当然不是“排序”。如果你能把“数组”放进代码语法中,就更容易理解了