2011-03-27 31 views
0

所以我的問題是,我無法獲得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切換。問題的另一個原因是爲什麼只重寫元素而不是切換元素?

+3

你是什麼意思切換元素?還有......改寫? – pajton 2011-03-27 19:26:34

+0

那麼什麼是期望的輸出? ^^ – 2016-12-09 01:51:34

回答

1

我不完全理解你想在年底前實現(因爲你還沒有告訴)是什麼,但我認爲,如果你重讀這段代碼:

temp = row; 
m[row][temp]=m[row][column]; 
m[row][column]= m[row][shufcol]; 
m[row][temp] = m[row][shufcol]; 

幾次嘗試用紙和筆執行它會發現錯誤。

0

如果我的理解,這將切換列和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; 
    } 
} 
0

這將切換其行內元素:

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

既然我不能完全確定你想要做什麼,這裏是整個一個開關元件rray隨機:

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