An array is an object in java.
If you try to print it by simply supplying it to System.out.print
statement, it 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 article will detail out different methods of printing array in java.
Method 1: Using Arrays.toString()
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
If you have a multi-dimensional array or a nested array, then use Arrays.deepToString()
method.|
Method 2: Using Arrays.asList()
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 list = Arrays.asList(array); System.out.println(list); } }
Above code prints
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 String.join()
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
With this approach, you can customize the separator between array elements.
Thus, if you want the array elements to be separated by a space, then supply a space as the first argument to the join method as shown below.
String joinedString = String.join(” “, array);
Method 4: Using java 8 streams
Java 8 also introduced the concept of streams which can be used to print a string array.
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
Above code prints array elements separated with an empty space.
If you want the elements to be separated by a comma, then modify the forEach
body as shown below.
Arrays.
asList(array).
stream().
forEach(s -> System.out.print(s + “, “));
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
With this approach also, you can change the separator between elements of the array.