-1

I know this isn't a difficult issue but I have come to a complete stand still, I want to print a two dimension array with values already assigned for example:

   int array1[][] = new int[1][1];
          array1[0][0] = 10;
          array1[0][1] = 20;
          array1[1][0] = 30;
          array1[1][1] = 40; 

I just want to simply print the values and I really can't remember how to do it, I keep on doing this

System.out.println(Arrays.toString(array1))`;

but I know this is wrong, can you help?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
D. Barnett
  • 123
  • 6
  • Not technically 'wrong', but looping through the values with nested for loops can help if you want the actual values. – Andrew Li Jul 01 '16 at 21:24
  • 1
    see http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array ( especially `System.out.println(Arrays.deepToString(deepArray));` ) – MacHala Jul 01 '16 at 21:28

1 Answers1

1

You may want to look at the Arrays.deepToString method, which is similar to Arrays.toString except that if the nested objects are arrays, it will recursively (and correctly) convert them into strings as well.

Also, note that there's a slight typo in your code - you need to size the array as

int[][] array1 = new int[2][2];

since you want a 2x2 array, not a 1x1 array.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065