5 import java.util.*; 6 7 class Matrix { 8 // declare the member field 9 private int[][] matrix; 10 12 public Matrix(int a, int b, int c, int d) { 13 matrix = new int[2][2]; 14 matrix[0][0] = a; 15 matrix[0][1] = b; 16 matrix[1][0] = c; 17 matrix[1][1] = d; 18 } 19 // identity matrix 20 public Matrix(char i) { 21 matrix = new int[2][2]; 22 matrix[0][0] = 1; 23 matrix[0][1] = 0; 24 matrix[1][0] = 1; 25 matrix[1][1] = 0; 26 } 27 29 public int[][] toPow(int n, int[][] matrix) { 30 if (n == 1) 31 return matrix; 32 else { 33 int[][] temp = matrix; 34 for (int i = 0; i < 2; i++) { 35 for (int j = 0; j < 2; j++) { 36 temp[i][j] += matrix[i][j] * this.matrix[j][i]; 37 } 38 } 39 return toPow(n - 1, temp); 40 } 41 } 42 public int[][] toPow(int n) { 43 return toPow(n, this.matrix); 44 } 45 } 46 47 class Maths { 48 49 public static void main(String[] args) { 55 Matrix m = new Matrix(1,2,3,4); 56 System.out.println(Arrays.toString(m.toPow(2))); 57 System.out.println(Arrays.toString(new int[][] {{1,2},{3,4}})); 58 } 59 }
Arrays.toString(Array) should be printing out the contents of the array when called. But when I tried to print the array in the last 2 lines of the code, I get the addresses instead of the content. Can anyone please help me understand why that is?
You’re seeing the results of calling
toString()on each element of your top array. But each element is itself an array. UseArrays.deepToString()instead: