Print Array in Java – Coding N Concepts



Print Array in Java – Coding N Concepts | ninjasquad

In this tutorial, we’ll learn how to print the elements of a given Array in Java.

Simplest Way

The Arrays.toString() and Arrays.deepToString() methods are the simplest way to print Arrays in Java and work well for all type of Arrays i.e. 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 simple Array

The Arrays.toString() method is the simplest way to print elements of an Array in a single line in Java:-

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

Limitation of Array.toString()

The Arrays.toString() method works well with 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 nested Array

The Arrays.deepToString() method can be used to print all the nested elements of a multi-dimensional Array in a single 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. Use Arrays.toString() and Arrays.deepToString() methods to convert an Array to a comma-separated String and print.
  2. Use Arrays.asList() and List.of() methods to convert an Array to a List and use its predefined toString() method to print elements in a comma-separated String.
  3. Use String.join() method to join elements of a String Array by any delimiter of your choice and print in a single line.
  4. Use Java Streams to map elements of an Array to String, join by any delimiter of your choice, and print in a single line.
  5. Use forEach loop to iterate through elements of an Array and print each element in 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: Internet

Leave a Comment

We are offering free coding tuts

X