0
public class practwo {
  public static void prac(int m[][]) {
    int rows = m.length;

    for (int i = 0; i < m[0].length; i++) {
      int t = m[0][i];
      m[0][i] = m[rows - 1][i];
      m[rows - 1][i] = t;
    }
  }

  public static void main(String[] args) {
    int m[][] = new int[4][4];
    m[][] = { {1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4} };

    prac(m);

    for (int i = 0; i < m.length; i++) {
      for (int j = 0; j < m[0].length; j++) {
        System.out.println(+i + j);
      }
    }
  }
}

Cannot figure out the error in the declaration line m[][]? I tried to swap first and last row of the matrix but showing error in the declaration line.

DWoldrich
  • 3,817
  • 1
  • 21
  • 19

1 Answers1

0

The way you are initializing the array is wrong.

int m[][] = new int[4][4];
m[][] = { {1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4} };

You can initialize the array in two ways if you know the composition of array elements.
Way 1:

int m[][] =  new int[][]{ {1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4} };

Way 2:

int m[][] = { {1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4} };
Manjunath H M
  • 818
  • 1
  • 10
  • 25