简体   繁体   English

用Java旋转指定的4x4列数组

[英]Rotating a specified column 4x4 array in Java

Basically, I have a 4x4 array with integers 0-15 initialised in it. 基本上,我有一个4x4数组,其中初始化了整数0-15。 This little task is to rotate the given column of the array so for example if the array is : 这个小任务是旋转数组的给定列,例如,如果数组为:

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

After application of rotateColumn(3), the array should look as follows: 应用rotateColumn(3)之后,数组应如下所示:

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

I have managed to implement the row rotation method and the code is: 我设法实现了行旋转方法,代码是:

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;
        }
    }

I tried a similar code for the column method but it didn't work out: 我为column方法尝试了类似的代码,但没有成功:

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;
    }

    }

The whole code for the Program is: 该程序的整个代码为:

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) {
    }

}

Could someone point out what I need to do right to make sure it works properly? 有人可以指出我需要做些正确的事以确保其正常工作吗? Than you very much :). 非常感谢您:)。

It seems you are confusing the memory layout of rows and columns. 看来您在混淆行和列的内存布局。 The data array is always an array of rows. 数据数组始终是行数组。 So you need to change how to address individual elements. 因此,您需要更改如何处理单个元素。 You need minor tweaking on your original method. 您需要对原始方法进行细微调整。 The following untested code derived from rotateRow() should make the difference. 以下来自rotateRow()的未经测试的代码应该有所作为。

    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;
        }
    }

How about this? 这个怎么样?

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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM