Date:

Share:

Print Array in Java – Coding N Concepts

Related Articles

In this tutorial, we will learn how to print the elements of a given array in Java.

The simplest way

God Arrays.toString() and Arrays.deepToString() Methods are the simplest way to print arrays in Java and work well for all types of arrays ie int, double, byte, String etc.

Quick examples
Arrays.toString(new int[]{1, 2, 3})
Arrays.deepToString(new int[][]{{1, 2}, {3, 4}, {5, 6}})
Arrays.deepToString(new int[][][]{{{1}, {2}, {3}}, {{4}, {5}}, {{6}}})

Arrays.toString() for a simple array

God Arrays.toString() The method is the simplest way to print array elements on a single line in Java:

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

Restriction of Array.toString()

God Arrays.toString() The method works well with a simple Array but prints ClassName@hashCode For nested arrays like this:-

String[][] nestedArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
System.out.println(Arrays.toString(nestedArray));
// prints something like [[Ljava.lang.String;@566776ad, [Ljava.lang.String;@6108b2d7]

Nothing to worry about! We have Arrays.deepToString() to the rescue.

Arrays.deepToString() for a nested array

God Arrays.deepToString() A method can be used to print all nested elements of a multidimensional array in one line in Java:

String[][] nestedArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
System.out.println(Arrays.deepToString(nestedArray));
// prints [[John, Mary], [Alice, Bob]]

String[][][] deepNestedArray = new String[][][] {{{"John"}, {"Mary"}, {"Alice"}}, {{"Bob"}, {"Adam"}}, {{"Emily"}}};
System.out.println(Arrays.deepToString(deepNestedArray));
// prints [[[John], [Mary], [Alice]], [[Bob], [Adam]], [[Emily]]]

Other ways

There are many other ways to print an array in Java:

  1. to use Arrays.toString() and Arrays.deepToString() Methods to convert an array to a comma-separated string and print.
  2. to use Arrays.asList() and List.of() Methods to convert an array to a list and use the predefined one toString() A method for printing elements in a comma-separated string.
  3. to use String.join() A method to concatenate elements of a string array by any delimiter of your choice and print on a single line.
  4. to use Java streams To map elements of an array to a string, join using any delimiter of your choice, and print on a single line.
  5. to use Each A loop to iterate over the elements of an array and print each element on a new line.

Let’s look at the examples:-

// Primitive int
int[] ints = new int[] {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(ints));
System.out.println(IntStream.of(ints).mapToObj(Integer::toString).collect(Collectors.joining(", ")));
System.out.println(IntStream.of(ints).boxed().map(Object::toString).collect(Collectors.joining(", ")));
IntStream.of(ints).forEach(System.out::println);
Arrays.stream(ints).forEach(System.out::println);

// Object String
String[] strs = new String[] {"John", "Mary", "Bob"};
System.out.println(Arrays.toString(strs));
System.out.println(Arrays.asList(strs));
System.out.println(List.of(strs));
System.out.println(String.join(", ", strs));
System.out.println(Stream.of(strs).collect(Collectors.joining(", ")));
Stream.of(strs).forEach(System.out::println);
Arrays.stream(strs).forEach(System.out::println);
Arrays.asList(strs).forEach(System.out::println);
List.of(strs).forEach(System.out::println);

// Object Enum 
DayOfWeek [] days = { FRIDAY, MONDAY, TUESDAY };
System.out.println(Arrays.toString(days));
System.out.println(Arrays.asList(days));
System.out.println(Stream.of(days).map(Object::toString).collect(Collectors.joining(", ")));
System.out.println(Arrays.toString(days));
Stream.of(days).forEach(System.out::println);
Arrays.stream(days).forEach(System.out::println);
Arrays.asList(days).forEach(System.out::println);
List.of(days).forEach(System.out::println);

Source

Popular Articles