Java OOP 改变二维矩阵的位置?

Java OOP Changing the position of 2d matrix?

如果我有一个矩阵,例如 3x4,其值为

1 2 3 4
5 6 7 8
9 1 2 3

假设用户想要旋转一列:
输入第 2 列。结果如下:

1 6 3 4
5 1 7 8
9 2 2 3

然后我想保存这个矩阵,以便我可以用另一种方法旋转它的行:
输入行号 = 2;那么结果将是:

1 6 3 4
1 7 8 5
9 2 2 3

public class Matrix
{
    int row;
    int column;

    int[][] arrays = new int[row][column];

    public Matrix(int row, int column)
    {
        this.row = row;
        this.column = column;
        this.arrays = new int [row][column];
    }

    public int getRow()
    {
        return row;
    }

    public void setRow(int row)
    {
        this.row = row;
    }

    public int getColumn()
    {
        return column;
    }

    public void setColumn(int column)
    {
        this.Column = column;
    }

    public int getArrays()
    {
        return arrays[row][column];
    }

    public void setArray(int row, int column)
    {
        this.row = row;
        this.column = column;
    }

    public void show()
    {
        int count = 0;
        for(int i = 0; i < this.row; i++)
        {
            for(int j = 0; j < this.column; j++)
            {
                if(count < 9)
                {
                    count += 1;
                    System.out.print(count);
                }
                else
                {
                    count = 0;
                    count += 1;
                    System.out.print(count);
                }
            }
            System.out.println("");
        }
    }

    public void rotateColumn(int column)
    {
        for(int i = 0; i < this.row; i++)
        {
            for(int j = 0; j < this.column; j++)
            {
                if(j == this.column - 1)
                {

                }
            }
        }
    }
}

我是 Java OOP 的新手,如果有人可以帮助我阐明和指导我的后续步骤,那将非常有帮助。提前致谢。

首先,我建议对提供的代码进行一些修改:

  1. 你有arrays,这很好,但你没有使用它/填充它(这是@tumisma在他的评论中的意思。)在使用下面的代码。
  2. 私有字段而不是包私有(无修饰符)
  3. 您不需要 rows/columns 的 setter,只需要一个构造函数
  4. 我会将填充和显示方法分开
  5. 您可以使用 count++;,这是 count += 1; / count = count + 1;
  6. 的更短方式
  7. 当您将计数重置为 1 时,您可以直接 (count = 1;) 而不是 count = 0; count += 1;
  8. 您不需要在 System.out.println(); 中为空行/回车输入任何内容。

这导致新代码:

public class Matrix
{
    private int amountOfRows;
    private int amountOfColumns;
    private int[][] matrixArray;

    public Matrix(int amountOfRows, int amountOfColumns)
    {
        this.amountOfRows = amountOfRows;
        this.amountOfColumns = amountOfColumns;
        matrixArray = new int[amountOfRows][amountOfColumns];

        fillMatrixArray();
    }

    private void fillMatrixArray()
    {
        int count = 0;
        for(int r = 0; r < amountOfRows; r++)
        {
            for(int c = 0; c < amountOfColumns; c++)
            {
                if(count < 9)
                {
                    count++;
                    matrixArray[r][c] = count;
                }
                else
                {
                    // If we reached 9, we reset back to 1
                    count = 1;
                    matrixArray[r][c] = count;
                }
            }
        }
    }

    public int[][] getMatrixArray()
    {
        return matrixArray;
    }

    public int getAmountOfRows()
    {
        return this.amountOfRows;
    }

    public int getAmountOfColumns()
    {
        return this.amountOfColumns;
    }

    public void show()
    {
        for(int r = 0; r < amountOfRows; r++)
        {
            for(int c = 0; c < amountOfColumns; c++)
            {
                System.out.print(matrixArray[r][c]);
            }
            System.out.println();
        }
        System.out.println();
    }

    // TODO: rotateColumn(int columnNr)

    // TODO: rotateRow(int rowNr)
}

那么,现在是您问题的主要部分:如何实现旋转 rows/columns 方法。我一开始想直接做,但是做起来很麻烦。相反,我创建了一个选定列的临时一维数组;然后逆时针旋转这些值一次;然后将它们放回矩阵中:

public void rotateColumn(final int columnNr)
{
    int columnIndex = columnNr - 1;

    // Values of the selected column in a temporary 1D-array
    int[] currentOrder = new int[amountOfRows];
    for (int r = 0; r < amountOfRows; r++)
    {
        currentOrder[r] = matrixArray[r][columnIndex];
    }

    // Rotate the values once counterclockwise
    int[] newOrder = new int[amountOfRows];
    for (int r = 0; r < amountOfRows; r++)
    {
        newOrder[r] = r == amountOfRows - 1
            ? currentOrder[0]
            : currentOrder[r + 1];

        // NOTE: This above is a shorter way for this:
        //if (r == amountOfRows - 1)
        //{
        //    newOrder[r] = currentOrder[0]
        //}
        //else
        //{
        //    newOrder[r] = currentOrder[r + 1]
        //}
    }

    // Replace the column in the matrix with this new ordered column
    for (int r = 0; r < amountOfRows; r++)
    {
        matrixArray[r][columnIndex] = newOrder[r];
    }
}

用法示例:

public static void main(final String[] args)
{
    Matrix matrix = new Matrix(3, 4);
    matrix.show();

    matrix.rotateColumn(2);
    matrix.show();
}

输出:

1234
5678
9123

1634
5178
9223

我将 rotateRow 方法的实现留给您。我确信有比将列放在单独的数组中、旋转它然后放回去更有效的方法/直接方法,但我无法找到它,所以我改用了这个解决方法。