猿问

如何使用翻转方法返回 Matrix 变量?

我写了以下代码:


public class Matrix

{

    private final int[][] array;

    private final int size1;

    private final int size2;

Matrix(int size1,int size2)

    {

        this.size1=size1;

        this.size2=size2;

        this.array=new int[size1][size2];

    }


    Matrix(int[][] color)

    {

        this.size1=color.length;

        this.size2=color[0].length;

        this.array= new int[size1][size2];


        for(int row=0;row<size1;row++)

        {

            for(int column=0;column<size2;column++)

            {

                this.array[row][column]=color[row][column];

            }

        }

    }


    }

    public Matrix flipVertically()

    {

     Matrix newArray=new Matrix(array);

    for(int row=size1-1;row>=0;row--)

    {

        for(int column=0;column<array[row].length;column++)

        {

           newArray[row][column]=array[size1][size2];     

    }

}

return newArray;

}

}

我的方法需要返回一个新的矩阵,其中的第一行成为最后一行,第二行成为倒数第二行,依此类推......

我写了这段代码,但我有两个问题:

  1. 当我写newArray[row][column]=array[size1][size2];它写给我:需要矩阵,但找到了数组。为什么要写它?我该如何解决?

  2. 返回“Metrix”变量是什么意思?它看起来怎么样?

谢谢 !


呼啦一阵风
浏览 153回答 2
2回答

慕妹3242003

您的问题是您正在将一个实例视为Matrix一个数组;它不是。它有一个数组。如果您想操作newArray实例的内部数组(我强烈建议将该变量重命名为newMatrix),那么您可以通过访问newArray.array.

POPMUISE

如果我在这里得到你就是你想要的:public class Matrix {&nbsp; private final int[][] array;&nbsp; private final int size1;&nbsp; private final int size2;&nbsp; Matrix(int size1, int size2) {&nbsp; &nbsp; this.size1 = size1;&nbsp; &nbsp; this.size2 = size2;&nbsp; &nbsp; this.array = new int[size1][size2];&nbsp; }&nbsp; @Override&nbsp; public String toString() {&nbsp; &nbsp; StringBuilder builder = new StringBuilder();&nbsp; &nbsp; for (int[] arr: array)&nbsp; &nbsp; &nbsp; builder.append(Arrays.toString(arr) + "\n");&nbsp; &nbsp; return "Matrix{" +&nbsp; &nbsp; &nbsp; &nbsp; "array=\n" + builder +&nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; }&nbsp; Matrix(int[][] color) {&nbsp; &nbsp; this.size1 = color.length;&nbsp; &nbsp; this.size2 = color[0].length;&nbsp; &nbsp; this.array = new int[size1][size2];&nbsp; &nbsp; for (int row = 0; row < size1; row++) {&nbsp; &nbsp; &nbsp; for (int column = 0; column < size2; column++) {&nbsp; &nbsp; &nbsp; &nbsp; this.array[row][column] = color[row][column];&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; public Matrix flipVertically() {&nbsp; &nbsp; int start = 0, end = size1 - 1;&nbsp; &nbsp; while (start < end) {&nbsp; &nbsp; &nbsp; int[] tmp = array[start];&nbsp; &nbsp; &nbsp; array[start] = array[end];&nbsp; &nbsp; &nbsp; array[end] = tmp;&nbsp; &nbsp; &nbsp; start++;&nbsp; &nbsp; &nbsp; end--;&nbsp; &nbsp; }&nbsp; &nbsp; return this;&nbsp; }&nbsp; public static void main(String[] args) {&nbsp; &nbsp; Matrix matrix = new Matrix(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}});&nbsp; &nbsp; System.out.println(matrix.flipVertically());&nbsp; &nbsp; matrix = new Matrix(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}});&nbsp; &nbsp; System.out.println(matrix.flipVertically());&nbsp; &nbsp; matrix = new Matrix(new int[][]{{1}});&nbsp; &nbsp; System.out.println(matrix.flipVertically());&nbsp; &nbsp; matrix = new Matrix(new int[][]{{1},{2},{3}});&nbsp; &nbsp; System.out.println(matrix.flipVertically());&nbsp; }输出:Matrix{array=[7, 8, 9][4, 5, 6][1, 2, 3]}Matrix{array=[10, 11, 12][7, 8, 9][4, 5, 6][1, 2, 3]}Matrix{array=[1]}Matrix{array=[3][2][1]}要回答您的问题:如上所述,您正在创建 Matrix 类的新实例,而不是数组。此外,这newArray[row][column]=array[size1][size2];不是您在 java 中复制数组的方式,尽管我认为您根本不需要复制它 - 请参阅我的实现。无论如何,如果要复制您的数组,请参阅此&nbsp;在 java 中复制 2d 数组您可以这样创建矩阵类型的变量:&nbsp;Matrix matrix = new Matrix(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}});.&nbsp;在我的代码中,我只是返回thiswitch 是对自身的引用。PS 改进代码的建议:添加 getter 和 setter,如果没有理由保留构造函数,则将其公开friendly,当您将空数组传递给构造函数时检查是否存在极端情况......
随时随地看视频慕课网APP

相关分类

Java
我要回答