[英]switching columns of a 2d array
所以我的問題是我無法像在單個變量數組中那樣能夠切換2d數組的元素。 而不是切換元素,而只是不斷地對其進行重寫...
for (int column = 0; column < m[0].length; column++) {
shufcol = (int)(Math.random()*4);
temp = column;
System.out.println(shufcol);
for(int row = 0; row < m.length; row++) {
temp = row;
m[row][temp]=m[row][column];
m[row][column]= m[row][shufcol];
m[row][temp] = m[row][shufcol];
}
}
輸入數組(3X4){{0 1 2 3} {1 4 5 6} {0 7 8 9}}
輸出數組{{2 2 3 2} {5 5 6 5} {8 8 9 8}}
如果您對math.random
感到好奇,那只是生成一個0到3之間的隨機列進行切換。 同樣,問題在於為什么它只重寫元素而不切換元素?
我不完全了解您最終想要實現的目標(因為您還沒有告知),但是我認為,如果您重新閱讀這段代碼:
temp = row;
m[row][temp]=m[row][column];
m[row][column]= m[row][shufcol];
m[row][temp] = m[row][shufcol];
幾次嘗試用紙和筆執行它,您會發現錯誤。
這將在其行內切換元素:
//input array (3X4) {{0 1 2 3} {1 4 5 6} {0 7 8 9}}
int[][] m = {{0, 1, 2, 3}, {1, 4, 5, 6}, {0, 7, 8, 9}};
for (int column = 0; column < m[0].length; column++) {
int shufcol = (int)(Math.random()*4);
int shufrow = (int)(Math.random()*3); //need row to switch with, too
for(int row = 0; row < m.length; row++) {
if(row == shufrow){
int tempField =m[row][column];
m[row][column]= m[row][shufcol];
m[row][shufcol] = tempField;
System.out.println("In row " + row + " : column " + column + " was switched with column " + shufcol + "!");
break; //just switch once per row, easier debugging ^^-d
}
}
}
//print output array
for(int row = 0; row < m.length; row++) {
String line = "\n";
for (int column = 0; column < m[0].length; column++) {
line += m[row][column] + " ";
}
System.out.print(line);
}
輸出:
In row 2 : column 0 was switched with column 2!
In row 0 : column 1 was switched with column 2!
In row 1 : column 2 was switched with column 3!
In row 0 : column 3 was switched with column 2!
0 2 3 1
1 4 6 5
8 7 0 9
由於我不確定您要做什么,因此這里是隨機切換整個數組中的元素:
//input array (3X4) {{0 1 2 3} {1 4 5 6} {0 7 8 9}}
int[][] m = {{0, 1, 2, 3}, {1, 4, 5, 6}, {0, 7, 8, 9}};
for (int column = 0; column < m[0].length; column++) {
int shufcol = (int)(Math.random()*4);
int shufrow = (int)(Math.random()*3); //need row to switch with, too
//there is no point in duplicating the count variable of a loop!
System.out.println(shufcol);
for(int row = 0; row < m.length; row++) {
//check that random field is not current field!
if(row != shufrow && column != shufcol){
//switch current with random
int temp = m[row][column]; // le backuppe
m[row][column] = m[shufrow][shufcol]; // write le new value of "random source field" to current field
m[shufrow][shufcol] = temp; // write value of current field to "random source field"
System.out.println("switched [" + row + "][" + column + "] with [" + shufrow + "]["+ shufcol + "]");
break; // use break to switch only once per row, easier debugging ^^-d
}
else {
System.out.println("will not switch with self!");
}
}
}
//print output array
for(int row = 0; row < m.length; row++) {
String line = "\n";
for (int column = 0; column < m[0].length; column++) {
line += m[row][column] + " ";
}
System.out.print(line);
}
產生以下輸出:
2
switched [0][0] with [2][2]
0
switched [0][1] with [1][0]
0
switched [0][2] with [2][0]
2
switched [0][3] with [1][2]
8 1 0 5
1 4 3 6
2 7 0 9
希望這可以幫助! ^^-d
據我了解,這將切換所有行的列和shufcol中的值(我沒有對其進行測試):
for (int column = 0; column < m[0].length; column++) {
shufcol = (int)(Math.random()*4);
System.out.println(shufcol);
for(int row = 0; row < m.length; row++) {
temp = m[row][shufcol];
m[row][shufcol] = m[row][column];
m[row][column] = temp;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.