How to print array in Java
Java array is a data structure where we can store the elements of the same data type. The elements of an array are stored in a contiguous memory location. So, we can store a fixed set of elements in an array.
There are following ways to print an array in Java:
Java for loop
Java for loop is used to execute a set of statements repeatedly until a particular condition is satisfied.
Example of for loop
In the following example, we have created an array of length four and initialized elements into it. We have used for loop for fetching the values from the array. It is the most popular way to print array in Java.
10 20 70 40
Java for-each loop
Java for-each loop is also used to traverse over an array or collection. It works on the basis of elements. It returns elements one by one in the defined variable.
Example of for-each loop
In the following example, we have created an array of String type of length four and initialized elements into it. We have used for-each loop to traverse over the array.
Delhi Jaipur Gujarat Mumbai
Java Arrays.toString() method
Java Arrays.toString() is a static method of Arrays class which belongs to java.util package It contains various methods for manipulating array.
It accepts an array of any primitive type as an argument. It returns a string representation of an array that contains a list of array's elements. The elements of an array are converted to String by String.valueOf(int) .
Example of toString() method
[34, -10, 56, -9, -33]
Java Arrays.deepToString() method
The deepToString() method of Java Arrays class is designed for converting multidimensional arrays to strings.
It accepts an array as a parameter. It returns the String representation of an array.
Example of deepToString() method
In the following example, we have created a two dimensional array of float type.
[[1.2, 2.5], [3.9, 4.0], [5.3, 6.2]]
Java Arrays.asList() method
Java Arrays.asList() is a static method of Java Arrays class which belongs to java.util package. It act as a bridge between array based and collection based API.
The method also provides an easy way to create a fixed-size list initialize to contain many elements.
It accepts an array as an argument. It returns the list view of an array.
Example of asList() method
[Hello, Java, Programmers]
Java Iterator interface
Java Iterator is an interface which belongs to java.util package. The Iterator object can be created by calling iterator() method. It is present in Collection interface. It returns an iterator.
Example of Iterator interface
In the following, example, we have declare an array and initialize elements into it. We first convert the specified array into list by using Arrays.asList() method because iterator allows us to traverse over the collection and then invoke iterator() method of collection class.
1.5 2.6 3.7 4.8 5.9
Java Stream API
A Java Stream is a data structure which is computed on-demand. It doesn't store data. It operates on the source data structure such as collection and array. Java stream API is used to implement internal iteration. It provides several features such as sequential and parallel execution.
Java stream() method
Java stream() is a static method of Java Arrays class which belongs to java.util package. It is used to get a sequential stream of an array.
Where T is the type of array. The method accepts an array whose elements are to be converted into a sequential stream. It returns a sequential IntStream with the specified array as its source.
Java forEach() method
It is a terminal operation. It does not guarantee to respect the encounter order of the stream.
The method accepts an action as a parameter. It is non-interfering action perform on each element. It does not return anything.
There are two terminal operations which we can apply to a stream to print an array.
Get an iterator to the stream
Example of stream.forEach() method
In the following example, we have used a different way to print an array. The forEach() method is used to iterate over every element of the stream. It is defined in the Iterable and Stream interface.
Inside the forEach() method we have used System.out which is a reference to an object. It represent standard output stream. It has a method called println(). It is an overloaded method which can accept anything as an argument. When we put println() method after member access operator (::), it becomes an expression.
Java C C++ Python Perl