0

I'm trying to initialize a 2d array using the braces notation as follows:

int[][] matrix = {{0, 2, 1}, {1, 2, 3}, {3, 2, 1}};
int[][] out = rotate(matrix);

for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
        System.out.print(matrix[i][j] + " ");
    }
    System.out.println("");
}

For the output, I am getting

0 0 0  
0 0 0  
0 0 0 

Can someone tell me the problem?

Abundance
  • 1,963
  • 3
  • 24
  • 46
  • 2
    The exact same code works correctly for me. Are you sure matrix isn't being modifed somewhere else? – eric.m Aug 19 '15 at 17:51
  • 1
    that is not true with just provided code, http://ideone.com/H9TM9h – jmj Aug 19 '15 at 17:51
  • Never mind. I passed the matrix into a function (I edited the code), and it seems to have modified the matrix, even though I thought it arguments were only passed by value. Thanks everyone. – Abundance Aug 19 '15 at 17:56
  • You might want to take a look to this http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?page=1&tab=oldest#tab-top – eric.m Aug 19 '15 at 17:59

0 Answers0