Trying to find how to print an Array in Java? As a beginner, you might struggle with arrays. In this quick tutorial, you will learn different ways to printing out an Array.
Here Are 6 Different Ways to Print Out an Array in Java
- Using For Loop in Java to print an Array
- Print Array using for Each-Loop/Enhanced For-Loop
- Java Arrays.toString() method to print an array
- Using Array.deepToString() method for print the values
- Using Arrays.asList() method
- Finally, print array using Java Iterator Interface
1. Using For Loop in Java to print an Array
The most common way of printing array in Java. For loops iterate to the end of the array and print each element.
For example, in the following program, we have created an integer array and used it to iterate and print the array.
public class PrintingArraysInJava1 {
public static void main(String[] args) {
int array[]= {2,5,7,8,9};
for(int i=0;i<array.length;i++)
{
System.out.print(array[i] + " ");//printing array using for loop
}
}
}
Output
2 5 7 8 9
2. Print Array Using For-Each Loop / Enhanced For-Loop
For-each loop is an enhanced version of the typical for-loop.
Similarly, it can be used to traverse over a collection or array and print out the array values. It returns one value at a time to the assigned variable.
Here is how we can print an array using for-each loop-
public class PrintingArraysInJava2 {
public static void main(String[] args) {
int array[]= {1,2,3,4,5};
for(int i:array)//using for each loop here
System.out.println(i);
}
}
Output
1 2 3 4 5
3. Print an Array using Array.toString() Method
Array.toString()
is a method of Array class in Java. It accepts an array as a parameter. It returns a String representation of an array.
Example of Array.toString()
import java.util.Arrays;
public class PrintingArraysInJava3 {
public static void main(String[] args) {
int array[]= {1,2,3,4,5};
System.out.println(Arrays.toString(array));
}
}
Output
[1, 2, 3, 4, 5]
4. Print out an Array using Array.deepToString() Method
This is also a method of Array class. Used for converting multidimensional arrays to string.
Below code example is provided for your help.
import java.util.Arrays;
public class PrintingArraysInJava4 {
public static void main(String[] args) {
int array[][]= {{2,-8},{3,4}};
System.out.println(Arrays.deepToString(array));
}
}
Output
[[2, -8], [3, 4]]
5. Using Arrays.asList() method
Arrays.asList()
is a static method of arrays class in Java. It accepts arrays as an argument. It returns a list view.
let us understand with a code snippet.
import java.util.Arrays;
public class PrintingArraysInJava5 {
public static void main(String[] args) {
String array[]= {"Welcome","to","letstacle"};
System.out.println(Arrays.asList(array));
}
}
Output
[Welcome, to, letstacle]
6. Java Iterator Interface
Iterator is an interface in Java. By calling the iterator method we can call its object.
Iterator allows traversing over collection. Therefore it is necessary to create a list from the array.Arrays.asList()
method helps us to convert it into a list.
Let us understand it by the following code snippet.
import java.util.Arrays;
import java.util.Iterator;
public class PrintingArraysInJava6 {
public static void main(String[] args) {
Integer[] array = { 1, 2, 3, 4, 5};
Iterator iter = Arrays.asList(array).iterator(); //will return iterator
while(iter.hasNext())
{
System.out.println(iter.next());
}
}
}
Output
1
2
3
4
5
✌️ Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed