How to print an array in java / Various ways to print an array in java

An array is an object in java. If you try to print it by simply supplying it to System.out.print statement, is is printed as just like any other object. Example,

String[] array = {"java", "python", "angular"};
System.out.print(array);

will print something like [Ljava.lang.String;@5fdef03a.
Since an array is not an object of a user defined class, you cannot override toString method to print the array in a meaningful format.


This post will detail out different methods in which an array can be printed.
Method 1: Using Arrays.toString method
java.util.Arrays class has a toString method which takes an array as argument and returns a string which is a representation of the array. This representation is meaningful and consists of elements of array surrounded by square brackets. Example,

import java.util.Arrays;
 
public class ArrayToStringConverter {
 
   public static void main(String[] args) {
      String[] array = {"java", "python", "angular"};
      System.out.println(Arrays.toString(array));
   }
}

Above code prints

[codippa, facebook, google]

If you have a multi-dimensional array or a nested array, then use Arrays.deepToString method.

Method 2: Using Arrays.asList method
java.util.Arrays class has a asList method which takes an array as argument and returns java.util.List object populated with the elements of array. This list can be directly printed as shown below.

import java.util.Arrays;
import java.util.List;
 
public class ArrayToStringConverter {
 
   public static void main(String[] args) {
      String[] array = {"java", "python", "angular"};
      // convert array to list
      List<String> list = Arrays.asList(array);
      System.out.println(list);
   }
}

Above code prints

[java, python, angular]

The reason that the list prints the above output is that the super class of all list implementations, that is, java.util.AbstractCollection has an overridden toString method which returns the list in string format as printed in the output.


Method 3: Using join method of String class
java 8 introduced a new method join in java.lang.String class. This is a static method which takes two arguments; second argument is the array of String elements and first argument is the delimiter or the separator with which the elements of array are joined.
This method iterates over the array elements, combines them with the supplied delimiter and returns the combined string. Example,

import java.util.Arrays;
 
public class ArrayToStringConverter {
 
   public static void main(String[] args) {
      String[] array = {"java", "python", "angular"};
      // join array elements with a comma
      String joinedString = String.join(", ", array);
      System.out.println(joinedString);
   }
}

Output is

java, python, angular

Method 4: Using streams in java 8
java 8 also introduced the concept of streams which can be used to print array contents. Example code which can be used to print array contents is given below.

import java.util.Arrays;
import java.util.stream.Stream;
 
public class ArrayToStringConverter {
 
   public static void main(String[] args) {
      String[] array = {"java", "python", "angular"};
      Arrays.asList(array).stream().forEach(s -> System.out.print(s+" "));
   }
}

Above code converts array to a list using asList method and then creates a stream of this list using stream method. This stream is then iterated using forEach method. In every iteration, the current array element is received into the lamba expression provided to forEach as argument. Output is

java, python, angular


Method 5: Using for loop
This is a traditional method which iterates over an array using a for loop and prints the current element in every iteration. This method is not a one-liner solution to print array. Example,

public class ArrayToStringConverter {
 
   public static void main(String[] args) {
      String[] array = {"java", "python", "angular"};
      // iterate over array
      for(String element: array) {
         System.out.print(element + " ");
      }
   }
}

Output is

java, python, angular

For any other method known to you, add in the comment box below. Keep visiting!!!

Mark Your Impression

Close Menu