An array is a data structure to store multiple elements of similar data types. Similar to other programming languages Java also supports Arrays. An Array is always stored in a contiguous location on the system memory. Java provides multiple methods of printing an Array basis on the requirements. We can directly access any array of elements with the index number or print the entire Array using Java loops.
In this tutorial, you’ll learn different techniques to print the elements of a given array in Java.
- Arrays.toString() method
- Arrays.deepToString() method
- for Loop
- for-each Loop
- Arrays.asList() method
Let’s discuss the above methods one by one including examples.
1. Java Arrays.toString() Method
The Arrays.toString() method is the simplest and frequently used method to print an Array in Java programming.
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Arrays; public class ArrayExample1 { public static void main(String[] args) { // Initialize an array String[] array = {"apple","orange","banana","grapes","mango"}; // Print array elements System.out.println(Arrays.toString(array)); } } |
Save above java program in ArrayExample1.java file and compile it and run. You will see the output like below:
Output:[apple, orange, banana, grapes, mango]
2. Using Arrays.deepToString() Method
Java Arrays.deepToString() method is used to fetch the deep content from an array. Here the deep content represents the multidimensional arrays.
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Arrays; public class ArrayExample2 { public static void main(String[] args) { //declaration and initialization of a multidimensional array int[][] array = {{1,2,3}, {4, 5}, {6, 7, 8}}; // Print array elements System.out.println(Arrays.deepToString(array)); } } |
Save above java program in ArrayExample2.java file and compile it and run. You will see the below output:
Output:[[1, 2, 3], [4, 5], [6, 7, 8]]
3. Using for Loop
The for loop is a frequently used method used for integrations. We can navigate through all array elements by the array index number. The following example will print array elements using for loop in Java.
1 2 3 4 5 6 7 8 9 10 11 | public class ArrayExample3 { public static void main(String[] args) { //declaration and initialization of an array String[] array = {"apple","orange","banana","grapes","mango"}; // Print array elements for(int i=0;i<array.length;i++) System.out.println(array[i]); } } |
Save above java program in ArrayExample3.java file and compile it and run. You will see the below output:
Output:apple orange banana grapes mango
4. Using for-each Loop
For-each is another way for traversing an array than for loop. Here is a quick example of using for-each to get array elements and print them.
1 2 3 4 5 6 7 8 9 10 11 12 | public class ArrayExample4 { public static void main(String[] args) { //declaration and initialization of an array String[] array = {"apple","orange","banana","grapes","mango"}; // Print array elements for (String element: array) { System.out.println(element); } } } |
Save program in a file named ArrayExample4.java. Then compile the program and run it. You will see the below output:
Output:apple orange banana grapes mango
5. Using Arrays.asList() Method
The asList() is another java function that returns a fixed-size list of a specified array.
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Arrays; public class ArrayExample5 { public static void main(String [] args) { //declaration and initialization of an array String[] array = {"apple","orange","banana","grapes","mango"}; // Print array elements System.out.println(Arrays.asList(array)); } } |
Save java program to file named ArrayExample5.java, then compile and run it. You will see the below output:
Output:[apple, orange, banana, grapes, mango]
Wrap Up
In this tutorial, you have learned 5 Java methods to print an array including examples.