Java Program To Print An Array.
In this tutorial, we will see how to print an array In Java.
We can print an array using the following 3 ways
- Print an array using for loop
- Print an array using
Arrays.toString()
- Print a multi-dimensional array using ` Arrays.deepToString()`
- Print an array using Java 8 Streams
1. Print an array using for loop
Here, we are iterating over an array and printing each element.
Java Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Java program to print an array using for loop
* By coderolls.com
*/
public class PrintArray {
public static void main(String[] args) {
int[] arr = {1, 5, 6, 9, 2, 6, 9, 6, 3, 2, 1};
System.out.println("Elements of the array are given below: ");
for (int i: arr) {
System.out.println(i);
}
}
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
Elements of the array are given below:
1
5
6
9
2
6
9
6
3
2
1
2. Print an array using Arrays.toString()
We have a ready-made method to print an array in Java.
The Arrays.toString()
method expects an array to be printed as an input parameter and returns a string of array elements.
Input param - array to be print
Returns - array string
Java Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* Java program to print an array using Arrays.toString()
* By coderolls.com
*/
public class PrintArray2 {
public static void main(String[] args) {
int[] arr = {1, 5, 6, 9, 2, 6, 9, 6, 3, 2, 1};
System.out.println("Elements of the array are given below: ");
String srrString = Arrays.toString(arr);
System.out.println(srrString);
}
}
Output
1
2
Elements of the array are given below:
[1, 5, 6, 9, 2, 6, 9, 6, 3, 2, 1]
3. Print a multi-dimensional array using Arrays.deepToString()
To print a multi-dimensional array we can use the ready-made Arrays.deepToString()
method.
Java Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Arrays;
/**
* Java program to print a multi-dimensional array using Arrays.deepToString()
* By coderolls.com
*/
public class PrintArray3 {
public static void main(String[] args) {
int[][] arr = {{1, 5, 8, 7}, {2, 6, 9, 6,}};
System.out.println("Elements of the multi-dimensional array are given below: ");
String srrString = Arrays.deepToString(arr);
System.out.println(srrString);
}
}
Output
1
2
Elements of the multi-dimensional array are given below:
[[1, 5, 8, 7], [2, 6, 9, 6]]
4. Print an array using Java 8 Streams
In Java 8 Streams, we can use the forEach()
method to print each element of an array.
Java Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Arrays;
/**
* Java program to print an array using Java 8 Streams forEach() method
* By coderolls.com
*/
public class PrintArray4 {
public static void main(String[] args) {
int[] arr = {1, 5, 8, 7, 2, 6, 9, 6};
System.out.println("Elements of the array are given below: ");
Arrays.stream(arr).forEach(System.out::println);
}
}
Output
1
2
3
4
5
6
7
8
9
Elements of the array are given below:
1
5
8
7
2
6
9
6
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!