What's the Simplest Way to Print a Java Array

What's the simplest way to print a Java array?

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 ]

What's the simplest way to print a Java array?

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 ]

Printing all elements of an array in one println statement in Java

That's the default implementation of toString() in Object you're seeing. You can use Arrays.toString for a readable result (make sure to import java.util.Arrays):

System.out.println(Arrays.toString(a));

How can I print an array easily?

You could use:
Arrays.toString(arr)
for normal arrays and/or
Arrays.deepToString(arr)
for arrays within arrays.
Both these methods return the string representation of the array.

See the Arrays docs for more.

How to print Array smart in java?

If you really want to customize your printing for variable arrays then why not do the following:

    public static String s(Object s) {
return s.toString();
}
public static String s(Object[] s) {
return Arrays.toString(s);
}
public static String s(Object[][] s) {
return Arrays.deepToString(s);
}
// For each primitive type of your single or multi dimension
// declare the following for either single or multi dimensional
public static String s(int[][] s) {
return Arrays.deepToString(s);
}
public static String s(int[] s) {
return Arrays.toString(s);
}

But keep in mind you are basically repackaging what the API already does.

how do I print my array using enhanced loops

Your code contains a few mistakes, and major are:

Fib[2] = Fib[0] + Fib[1]; this should be Fib[i] = Fib[i - 1] + Fib[i - 2];

Another mistake is nesting for loops. The enhanced for loop should be after the loop with index i.

int[] Fib = new int[20];
Fib[0] = 0;
Fib[1] = 1;

for (int i = 2; i < Fib.length; i++) {
Fib[i] = Fib[i - 1] + Fib[i - 2];
}

for (int number : Fib) {
System.out.print(" " + number);
}

You can use Arrays.toString(Fib) instead of using a loop for printing the array.



Related Topics



Leave a reply



Submit