Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用flip方法返回矩阵变量?_Java_Arrays_Matrix_Dimension - Fatal编程技术网

Java 如何使用flip方法返回矩阵变量?

Java 如何使用flip方法返回矩阵变量?,java,arrays,matrix,dimension,Java,Arrays,Matrix,Dimension,我编写了以下代码: 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];

我编写了以下代码:

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;
}
}
公共类矩阵
{
专用最终int[][]数组;
私人最终国际标准尺寸1;
私人最终国际规模2;
矩阵(整数大小1,整数大小2)
{
this.size1=size1;
this.size2=size2;
this.array=newint[size1][size2];
}
矩阵(int[][]颜色)
{
this.size1=color.length;
this.size2=color[0]。长度;
this.array=newint[size1][size2];

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

来执行此操作,如果我现在就告诉您,这就是您想要的:

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

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();

    for (int[] arr: array)
      builder.append(Arrays.toString(arr) + "\n");

    return "Matrix{" +
        "array=\n" + builder +
        '}';
  }

  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() {
    int start = 0, end = size1 - 1;

    while (start < end) {
      int[] tmp = array[start];
      array[start] = array[end];
      array[end] = tmp;
      start++;
      end--;
    }

    return this;
  }

  public static void main(String[] args) {
    Matrix matrix = new Matrix(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}});
    System.out.println(matrix.flipVertically());
    matrix = new Matrix(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}});
    System.out.println(matrix.flipVertically());
    matrix = new Matrix(new int[][]{{1}});
    System.out.println(matrix.flipVertically());
    matrix = new Matrix(new int[][]{{1},{2},{3}});
    System.out.println(matrix.flipVertically());
  }
回答您的问题:

  • 如上所述,您正在创建Matrix类的新实例,而不是 一个数组。另外,这个
    newArray[row][column]=array[size1][size2];
    我不认为你可以用java复制数组 需要复制它-请参阅我的实现。如果要复制,请无论如何 您的阵列,请参考此
  • 矩阵类型的变量您可以创建此 方法:
    矩阵矩阵=新矩阵(新int[][{{1,2,3},{4,5,6},
    {7,8,9}});
    在我的代码中,我只返回
    这个
    开关是对 本身

  • 改进代码的建议:添加getter和setter,在没有理由的情况下公开构造函数使其友好,检查传递给构造函数的空数组时是否存在角落情况…

    可能的重复项。看看这是否有帮助。还有你的
    newArray[row][column]=array[size1][size2]
    ,如果for循环工作正常,将把
    数组
    中的最后一项放入
    新数组
    的每个插槽中。这样就不会翻转矩阵。
    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]
    }