0

i wrote the following code :

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

and my method needs to return a new Matrix which inside of it, the first row becomes the last row, the second row becomes the second last row and so on...

i wrote this code but i have 2 problems:

  1. when i write newArray[row][column]=array[size1][size2]; it writes me : Matrix required but array found. why does it writes it? and how can i fix it?

  2. what does it means to return "Metrix" variable? how does it looks like?

Thank you !

Daniel
  • 1
  • Possible duplicate of https://stackoverflow.com/q/15007264/5184092. See if that helps you out. – luckyging3r Jan 18 '19 at 23:33
  • Also your `newArray[row][column]=array[size1][size2]`, if you get your for loop working correctly, will put the last item in `array` into every slot of `newArray`. Thus not flipping your matrix. – luckyging3r Jan 18 '19 at 23:36

2 Answers2

1

Your problem is that you are treating an instance of Matrix like it is an array; it is not. It has an array. If you want to manipulate the internal array of the newArray instance (I highly recommend renaming that variable to newMatrix), then you can do so by accessing newArray.array.

Josh
  • 500
  • 3
  • 6
0

If I get you right here is what you want:

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());
  }

Output:

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

To answer your questions:

  1. As mentioned above you are creating a new instance of Matrix class, not an array. Also, this newArray[row][column]=array[size1][size2]; is not how you can copy an array in java, though I don't think you need to copy it at all - see my implementation. Anyway if want to copy your array, please refer to this copy a 2d array in java
  2. Variable of type matrix you can create this way: Matrix matrix = new Matrix(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}});. In my code I just return this witch is a reference to itself.

P.S. Suggestions for improvement of your code: add getters and setters, make your constructor public if there is no reason keep it friendly, check for corner cases when you have empty array passed to constructor....

Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28