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

标签 java oop matrix 2d

如果我有一个矩阵,例如 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. 你有数组,这很好,但你没有使用它/填充它(这就是@tumisma他的评论的意思。)在代码中下面使用它。
  2. 私有(private)字段而不是包私有(private)(无修饰符)
  3. 您不需要行/列的 setter ,只需要构造函数
  4. 我会将 fill 和 show 方法分开
  5. 您可以使用 count++;,这是 count += 1;/count = count + 1; 的更短方式
  6. >
  7. 当您将计数重置为 1 时,您可以直接执行此操作 (count = 1;),而不是 count = 0;计数 += 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)
}

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

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 方法的实现留给您。我确信有一种比将列放入单独的数组中、旋转它然后放回去更有效的方法/直接方法,但我无法找到它,所以我使用了这个解决方法。

关于Java OOP 改变二维矩阵的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35940538/

相关文章:

java - Spring重定向到web.xml配置中的另一个url?

java - 如何使用 javafx 隐藏或停用 TextField 和 Label

java - 在 CDI 实现项目中包含空 beans.xml 的目的是什么?

java - 如何限制 netty 用于客户端连接的线程

java - 实现 map 功能

python - 方法和函数之间的区别,在 Python 中与 C++ 相比

php - 如何给大量类方法 "almost"相同的代码

c++ - 使用 xptr 在内存中存储和检索矩阵

java - 使用 java 使用字符串和矩阵生成特定模式

r - 如何快速将矩阵列表乘以向量列表?