Skip to main content
Stack Overflow for Teams is now Stack Internal: See how we’re powering the human intelligence layer of enterprise AI. Read more >
Update link of documentation to latest LTS
Source Link
Marc Bannout
  • 451
  • 1
  • 6
  • 17

Since Java 5 you can use Arrays.toString(arr)Arrays.toString(arr) or Arrays.deepToString(arr)Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

    String[] array = new String[] {"John", "Mary", "Bob"};
    System.out.println(Arrays.toString(array));
    

    Output:

    [John, Mary, Bob]
    
  • Nested Array:

    String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
    // Gives undesired output:
    System.out.println(Arrays.toString(deepArray));
    // Gives the desired output:
    System.out.println(Arrays.deepToString(deepArray));
    

    Output:

    [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
    [[John, Mary], [Alice, Bob]]
    
  • double Array:

    double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
    System.out.println(Arrays.toString(doubleArray));
    

    Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
    
  • int Array:

    int[] intArray = { 7, 9, 5, 1, 3 };
    System.out.println(Arrays.toString(intArray));
    

    Output:

    [7, 9, 5, 1, 3 ]
    

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

    String[] array = new String[] {"John", "Mary", "Bob"};
    System.out.println(Arrays.toString(array));
    

    Output:

    [John, Mary, Bob]
    
  • Nested Array:

    String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
    // Gives undesired output:
    System.out.println(Arrays.toString(deepArray));
    // Gives the desired output:
    System.out.println(Arrays.deepToString(deepArray));
    

    Output:

    [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
    [[John, Mary], [Alice, Bob]]
    
  • double Array:

    double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
    System.out.println(Arrays.toString(doubleArray));
    

    Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
    
  • int Array:

    int[] intArray = { 7, 9, 5, 1, 3 };
    System.out.println(Arrays.toString(intArray));
    

    Output:

    [7, 9, 5, 1, 3 ]
    

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

    String[] array = new String[] {"John", "Mary", "Bob"};
    System.out.println(Arrays.toString(array));
    

    Output:

    [John, Mary, Bob]
    
  • Nested Array:

    String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
    // Gives undesired output:
    System.out.println(Arrays.toString(deepArray));
    // Gives the desired output:
    System.out.println(Arrays.deepToString(deepArray));
    

    Output:

    [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
    [[John, Mary], [Alice, Bob]]
    
  • double Array:

    double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
    System.out.println(Arrays.toString(doubleArray));
    

    Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
    
  • int Array:

    int[] intArray = { 7, 9, 5, 1, 3 };
    System.out.println(Arrays.toString(intArray));
    

    Output:

    [7, 9, 5, 1, 3 ]
    
Just extracting some additional output from the code fragment to be consistent.
Source Link
YoYo
  • 9.5k
  • 8
  • 62
  • 75

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

    String[] array = new String[] {"John", "Mary", "Bob"};
    System.out.println(Arrays.toString(array));
    

    Output:

    [John, Mary, Bob]
    
  • Nested Array:

    String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
    // Gives undesired output:
    System.out.println(Arrays.toString(deepArray));
    //output: [[Ljava.lang.String;@106d69c,Gives [Ljava.lang.String;@52e922]the desired output:
    System.out.println(Arrays.deepToString(deepArray));
    

    Output:

    [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
    [[John, Mary], [Alice, Bob]]
    
  • double Array:

    double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
    System.out.println(Arrays.toString(doubleArray));
    

    Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
    
  • int Array:

    int[] intArray = { 7, 9, 5, 1, 3 };
    System.out.println(Arrays.toString(intArray));
    

    Output:

    [7, 9, 5, 1, 3 ]
    

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

    String[] array = new String[] {"John", "Mary", "Bob"};
    System.out.println(Arrays.toString(array));
    

    Output:

    [John, Mary, Bob]
    
  • Nested Array:

    String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
    System.out.println(Arrays.toString(deepArray));
    //output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
    System.out.println(Arrays.deepToString(deepArray));
    

    Output:

    [[John, Mary], [Alice, Bob]]
    
  • double Array:

    double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
    System.out.println(Arrays.toString(doubleArray));
    

    Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
    
  • int Array:

    int[] intArray = { 7, 9, 5, 1, 3 };
    System.out.println(Arrays.toString(intArray));
    

    Output:

    [7, 9, 5, 1, 3 ]
    

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

    String[] array = new String[] {"John", "Mary", "Bob"};
    System.out.println(Arrays.toString(array));
    

    Output:

    [John, Mary, Bob]
    
  • Nested Array:

    String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
    // Gives undesired output:
    System.out.println(Arrays.toString(deepArray));
    // Gives the desired output:
    System.out.println(Arrays.deepToString(deepArray));
    

    Output:

    [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
    [[John, Mary], [Alice, Bob]]
    
  • double Array:

    double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
    System.out.println(Arrays.toString(doubleArray));
    

    Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
    
  • int Array:

    int[] intArray = { 7, 9, 5, 1, 3 };
    System.out.println(Arrays.toString(intArray));
    

    Output:

    [7, 9, 5, 1, 3 ]
    
Fixed indentation of the output, removed redundant padding and removed the import statement. The package is already available in the link.
Source Link
Ivar
  • 7k
  • 12
  • 58
  • 69

Since Java 5 you can import java.util.Arrays; and then use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

      String[] array = new String[] {"John", "Mary", "Bob"};
      System.out.println(Arrays.toString(array));
    

    Output:

    [John, Mary, Bob]
    

Output:

    [John, Mary, Bob]
  • Nested Array:

      String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
      System.out.println(Arrays.toString(deepArray));
      //output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
      System.out.println(Arrays.deepToString(deepArray));
    

    Output:

    [[John, Mary], [Alice, Bob]]
    

Output:

    [[John, Mary], [Alice, Bob]]
  • double Array:

      double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
      System.out.println(Arrays.toString(doubleArray));
    

    Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
    

Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
  • int Array:

      int[] intArray = { 7, 9, 5, 1, 3 };
      System.out.println(Arrays.toString(intArray));
    

    Output:

    [7, 9, 5, 1, 3 ]
    

Output:

    [7, 9, 5, 1, 3 ]

Since Java 5 you can import java.util.Arrays; and then use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

      String[] array = new String[] {"John", "Mary", "Bob"};
      System.out.println(Arrays.toString(array));
    

Output:

    [John, Mary, Bob]
  • Nested Array:

      String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
      System.out.println(Arrays.toString(deepArray));
      //output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
      System.out.println(Arrays.deepToString(deepArray));
    

Output:

    [[John, Mary], [Alice, Bob]]
  • double Array:

      double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
      System.out.println(Arrays.toString(doubleArray));
    

Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
  • int Array:

      int[] intArray = { 7, 9, 5, 1, 3 };
      System.out.println(Arrays.toString(intArray));
    

Output:

    [7, 9, 5, 1, 3 ]

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you're asking.

Examples:

  • Simple Array:

    String[] array = new String[] {"John", "Mary", "Bob"};
    System.out.println(Arrays.toString(array));
    

    Output:

    [John, Mary, Bob]
    
  • Nested Array:

    String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
    System.out.println(Arrays.toString(deepArray));
    //output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
    System.out.println(Arrays.deepToString(deepArray));
    

    Output:

    [[John, Mary], [Alice, Bob]]
    
  • double Array:

    double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
    System.out.println(Arrays.toString(doubleArray));
    

    Output:

    [7.0, 9.0, 5.0, 1.0, 3.0 ]
    
  • int Array:

    int[] intArray = { 7, 9, 5, 1, 3 };
    System.out.println(Arrays.toString(intArray));
    

    Output:

    [7, 9, 5, 1, 3 ]
    
the import statement for 'Arrays' should be provided in this answer as well.
Source Link
questionto42
  • 9.9k
  • 8
  • 80
  • 125
Loading
deleted 53 characters in body
Source Link
azro
  • 54.2k
  • 9
  • 38
  • 75
Loading
Rollback to Revision 12 - Edit approval overridden by post owner or moderator
Source Link
Esko
  • 29.5k
  • 11
  • 58
  • 83
Loading
add llinks to documentation
Source Link
diralik
  • 7.4k
  • 4
  • 34
  • 55
Loading
formatting improvements (break up code blocks)
Source Link
randers
  • 5.2k
  • 6
  • 41
  • 64
Loading
fixed wording, marked code. also: we don't need to add a remark about which packages to include in each and every post..
Source Link
Georg Plaz
  • 6k
  • 5
  • 43
  • 66
Loading
Rollback to Revision 6
Source Link
Pshemo
  • 124.5k
  • 25
  • 194
  • 280
Loading
int count = 0; for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); count++; if (count == 10) { System.out.println(); count = 0; } }
Source Link
Saddam Abu Ghaida
  • 6.8k
  • 2
  • 24
  • 29
Loading
int count = 0; for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); count++; if (count == 10) { System.out.println(); count = 0; } }
Source Link
Loading
Added double and int support
Source Link
Naveed Ahmad
  • 6.8k
  • 2
  • 63
  • 84
Loading
For beginners like myself, this would be highly appreciated (might be irritating for advanced users however)
Source Link
mleko
  • 12.3k
  • 7
  • 53
  • 71
Loading
For beginners like myself, this would be highly appreciated (might be irritating for advanced users however)
Source Link
Loading
take out weird sentence fragment
Source Link
Barett
  • 6k
  • 6
  • 54
  • 57
Loading
Explain necessary import for noobs.
Source Link
Richard Cotrina
  • 2.6k
  • 1
  • 25
  • 23
Loading
Post Made Community Wiki by Esko
Source Link
Esko
  • 29.5k
  • 11
  • 58
  • 83
Loading