【问题标题】:Java OOP Changing the position of 2d matrix?Java OOP 改变二维矩阵的位置?
【发布时间】:2016-03-11 12:40:45
【问题描述】:

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

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

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

1 6 3 4
5 1 7 8
9223

然后我想保存这个矩阵,以便我可以用另一种方法旋转它的行:
输入行号 = 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, 2, 3, 4], [5, 6, 7, 8], [9, 1, 2, 3]] 而是这个 [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]??

标签: java oop matrix 2d


【解决方案1】:

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

  1. 你有arrays,这很好,但你没有使用它/填充它(这是@tumisma 对他的评论的意思。)在下面的代码中使用它。
  2. 私有字段而不是包私有(无修饰符)
  3. 您不需要用于行/列的设置器,只需要一个构造器
  4. 我会将填充和显示方法分开
  5. 您可以使用count++;,这是count += 1; / count = count + 1; 的更短方式
  6. 当您将计数重置为 1 时,您可以直接执行此操作 (count = 1;) 而不是 count = 0; count += 1;
  7. 您无需在 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-方法的实现留给你。我确信有一种比将列放在单独的数组中、旋转它然后放回去更有效的方法/直接方法,但是我找不到它,所以我改用了这个解决方法。

【讨论】:

  • 非常感谢您的帮助和edit@Kevin,您能否为我提供一些建议,以便为此类问题创建自己的解决方案?您是否使用反复试验或如何分解问题以找到您编写的解决方案。再次感谢您。
  • 对不起,如果我宠坏了太多。我的第一种方法是尝试在现有 2D 数组本身内旋转列。这并没有真正奏效,因为我要么完全错了(比如整个专栏变成了2 2 2,要么除了最后一个数字之外一切都是正确的)。我还尝试在这种方法中进行反向 for 循环 (for (int r = amountOfRows - 1; r &gt;= 0; r--)),但这也没有用。然后我想出了柱的隔离,旋转它,然后重新插入它,这很容易,虽然没有我想要的效率(考虑到 3 个 for 循环)。
  • 要添加到我上面的评论中,是的,一开始我有点反复试验,但由于花了一点时间,我寻找了一种不太理想但更易于实施的方法,这就是我在回答中所展示的。它仅在rotateColumn-方法中执行了 3 个 for 循环,所以我对解决方案并不满意,也许其他人对您的问题有更好的性能方法。但它可以工作,并且在循环和小网格(
  • 知道了!谢谢你的指导。祝你有美好的一天:) @Kevin
猜你喜欢
  • 2013-10-30
  • 1970-01-01
  • 2017-06-06
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多