2016-04-14 3 views
0

Grundsätzlich habe ich ein 4x4 Array mit ganzen Zahlen 0-15 in ihm initialisiert. Diese kleine Aufgabe ist es, die gegebene Spalte des Arrays so zum Beispiel zu drehen, wenn das Array ist:Rotieren einer angegebenen Spalte 4x4 Array in Java

14 0 1 12 
    13 4 2 3 
    7 6 11 8 
    5 15 9 10 

Nach der Anwendung von rotateColumn (3), sollte das Array wie folgt aussehen:

 14 0 1 10 
     13 4 2 12 
     7 6 11 3 
     5 15 9 8 

I die Zeilenrotationsverfahren zu implementieren, und der Code ist es gelungen:

public static void rotateRow(int[][] arr, int row) { 
     int newCurrent = arr[row][arr.length - 1]; 
     int nextCurrent; 
     for (int currentIndex = 0; currentIndex < arr.length; currentIndex++) { 
      nextCurrent = arr[row][currentIndex]; 
      arr[row][currentIndex] = newCurrent; 
      newCurrent = nextCurrent; 
     } 
    } 

ich einen ähnlichen Code für die Spalte Methode versucht, aber es hat nicht funktioniert:

public static void rotateColumn(int[][] arr, int column) { 
    int newCurrent1 = arr[column][arr.length - 1]; 
    int nextCurrent1 ; 
    for (int currentIndex1 = 0; currentIndex1 < arr.length; currentIndex1++){ 
     nextCurrent1 = arr[column][currentIndex1]; 
     arr[column][currentIndex1] = newCurrent1; 
     newCurrent1 = nextCurrent1; 
    } 

    } 

Der gesamte Code für das Programm ist:

public class Puzzle { 

    public static final int N = 4; 
    public static final int NUMBER_OF_ROTATIONS = 5; 

    public static void main(String[] args) { 
     int[][] puzzle = new int[N][N]; 
     reset(puzzle); 
     test(puzzle); 
     reset(puzzle); 
     scramble(puzzle); 
     System.out.println("### Testing puzzle game play\n"); 
     play(puzzle); 
    } 

    public static void print(int[][] puzzle) { 
     for (int[] row : puzzle) { 
      for (int elem : row) { 
       System.out.printf("%4d", elem); 
      } 
      System.out.println(); 
     } 
     System.out.println(); 
    } 

    public static void test(int[][] puzzle) { 
     System.out.println("### Testing reset method\n"); 
     print(puzzle); 
     System.out.println("### Testing rotate methods\n"); 
     print(puzzle); 
     for (int i = 0; i < N; i++) { 
      System.out.println("### rotateColumn(" + i + ")\n"); 
      rotateColumn(puzzle, i); 
      print(puzzle); 
      System.out.println("### rotateRow(" + i + ")\n"); 
      rotateRow(puzzle, i); 
      print(puzzle); 
     } 
     reset(puzzle); 
     System.out.println("### Testing random rotations\n"); 
     print(puzzle); 
     for (int i = 0; i < 5; i++){ 
      randomRotation(puzzle); 
      print(puzzle); 
     } 
    } 

    public static void reset(int[][] puzzle) { 
     for (int i = 0; i < N; i++) { 
      for (int j = 0; j < N; j++) 
       puzzle[i][j] = i * N + j; 
     } 
    } 

    public static void scramble(int[][] puzzle) { 
     for (int i = 0; i < NUMBER_OF_ROTATIONS; i++) { 
      randomRotation(puzzle); 
     } 
    } 




    public static void rotateRow(int[][] arr, int row) { 
     int newCurrent = arr[row][arr.length - 1]; 
     int nextCurrent; 
     for (int currentIndex = 0; currentIndex < arr.length; currentIndex++) { 
      nextCurrent = arr[row][currentIndex]; 
      arr[row][currentIndex] = newCurrent; 
      newCurrent = nextCurrent; 
     } 
    } 





    // TODO: Implement method as specified in assignment brief 

    public static void rotateColumn(int[][] arr, int column) { 
    int newCurrent1 = arr[column][arr.length - 1]; 
    int nextCurrent1 ; 
    for (int currentIndex1 = 0; currentIndex1 < arr.length; currentIndex1++){ 
     nextCurrent1 = arr[column][currentIndex1]; 
     arr[column][currentIndex1] = newCurrent1; 
     newCurrent1 = nextCurrent1; 
    } 

    } 


    // TODO: Implement method as specified in assignment brief 

    public static void randomRotation(int[][] puzzle) { 
    } 

    // TODO: Implement method as specified in assignment brief 

    static void play(int[][] puzzle) { 
    } 

} 

Könnte jemand darauf hinweisen, was ich brauche, das Richtige zu tun, um sicherzustellen, funktioniert es richtig? Als du sehr viel :).

+0

Was ist die Ausgabe, die Sie erhalten? – robotlos

+0

Die Ausgabe, die ich bekomme, ist das, obwohl die vorherige Reihenrotation die gleiche bleibt, anstatt dass die Spalte rotiert wird, werden Zufallszahlen mit den ganzen Zahlen in der Spalte ausgetauscht, wenn ich nur die ganzen Zahlen in dieser Spalte zwischen ihnen tauschen will. –

+0

Ich habe versucht, Ihre 'rotateColumn' auszuführen, ich bekomme eine Ausnahme außerhalb des Bereichs bei 'int newCurrent1 = arr [Spalte] [arr.length - 1];' – robotlos

Antwort

0

Wie wäre es damit?

public void rotateColumn(int[][] arr, int column) { 
    int[] col = new int[arr.length]; 
    for(int row=0; row<arr.length; row++) { 
     col[row] = arr[row][column]; 
    } 
    for(int row=0; row<arr.length; row++) { 
     arr[row][column] = col[(row==0) ? arr.length-1 : row-1]; 
    } 
} 
+0

Schließen, aber das dreht sich falsch. – robotlos

+0

@Daniel ahhh Kumpel Ich probierte das, aber das ändert das ganze Array :(. –

+0

So sollte es den richtigen Weg drehen (bearbeitete meine erste Antwort) –

1

Es scheint, dass Sie das Speicherlayout von Zeilen und Spalten verwirren. Das Datenarray ist immer ein Array von Zeilen. Sie müssen also ändern, wie Sie einzelne Elemente ansprechen. Sie müssen kleinere Änderungen an Ihrer ursprünglichen Methode vornehmen. Der folgende ungesteckte Code, der von rotateRow() abgeleitet wurde, sollte den Unterschied ausmachen.

public static void rotateColumn(int[][] arr, int col) { 
     int newCurrent = arr[arr.length - 1][col]; 
     int nextCurrent; 
     for (int currentIndex = 0; currentIndex < arr.length; currentIndex++) { 
      nextCurrent = arr[currentIndex][col]; 
      arr[currentIndex][col] = newCurrent; 
      newCurrent = nextCurrent; 
     } 
    }