Read Also: Top 50 Java Array Interview Questions and Answers
Now, we will see different ways of printing arrays in Java with code examples.
1. Using for loop and for-each loop for,
a. One-dimensional array.
b. Two-dimensional array.
c. Three-dimensional array.
2. Using Arrays.asList() method.
3. Using Arrays.toString() method.
4. Using Arrays.deepToString() method.
5. Using the Iterator interface.
6. Using Stream API.
1. Using for loop and for-each loop
In Java, for loop is used to traverse the array elements in both forward and backward directions.
for-each loop is used to iterate the array elements only in the forward direction.
Using a for-each loop, we cannot access the array elements using index number and modify the array as we do in for loop.
Code examples for one, two, and three-dimensional arrays using for loop and for-each loop are given below.
1.a One dimensional array using for loop and for-each loop:
public class PrintOneDimensionalArray {
public static void main(String args[]) {
//Declare and initialize one dimensional integer Array
int intArray[]={1,2,3,4,5};
/* or Declare and initialize one dimensional integer array as below,
* intArray[]=new int[5];
* intArray[0]=1;
* intArray[1]=2;
* intArray[2]=3;
* intArray[3]=4;
* intArray[4]=5;
*/
/*
* Accessing one dimensional integer array elements using for-each loop
* System.out.println("Elements of integer array are");
* for(int i:intArray) {
* System.out.println(i);
* }
*/
//Accessing one dimensional integer array elements using for loop
System.out.println("The output of one dimensional integer array is");
for(int i=0;i<intArray.length;i++) {
System.out.println(intArray[i]);
}
}
}
Output:
The output of a one-dimensional integer array is
1
2
3
4
5
1.b Two-dimensional array using for loop and for-each loop:
public class PrintTwoDimensionalArray{
public static void main(String args[]) {
//Declare and initialize two dimensional integer Array.
int intArray[][]={{1,2,1},{3,4,3},{5,6,7}};
/* or Declare and initialize two dimensional integer array as below,
* intArray[][]=new int[3][3];
* intArray[0][0]=1;
* intArray[0][1]=2;
* intArray[0][2]=1;
* intArray[1][0]=3;
* intArray[1][1]=4;
* intArray[1][2]=3;
* intArray[2][0]=5;
* intArray[2][1]=6;
* intArray[2][2]=7;
*/
/* Accessing two-dimensional integer array elements using for-each loop
System.out.println("The output of two dimensional integer array is");
for(int i[]:intArray) {
for(int j:i) {
System.out.print(j+ " ");
}
System.out.println();
}
*/
//Accessing two dimensional integer array elements using for loop
System.out.println("The output of two dimensional integer array is");
for(int i=0;i<intArray.length;i++) {
for(int j=0;j<intArray.length;j++) {
System.out.print(intArray[i][j]+" ");
}
System.out.println();
}
}
}
Output:
The output of a two-dimensional integer array is
1 2 1
3 4 3
5 6 7
1.c Three-dimensional array using for loop and for-each loop:
public class PrintThreeDimensionalArray {
public static void main(String args[]) {
//Declare and initialize three dimensional integer Array.
int intArray[][][]={{{1,2},{3,4}},{{3,3},{4,4}}};
/* or Declare and initialize three dimensional integer array as
below,
* int intArray[][][]=new int[2][2][2];
* intArray[0][0][0]=1;
* intArray[0][0][1]=2;
* intArray[0][1][0]=3;
* intArray[0][1][1]=4;
* intArray[1][0][0]=3;
* intArray[1][0][1]=3;
* intArray[1][1][0]=4;
* intArray[1][1][1]=4;
*/
/* Accessing three-dimensional integer array elements using for-each
loop
System.out.println("The output of three dimensional
integer array is");
for(int i[][]:intArray) {
for(int j[]:i) {
for(int z:j) { System.out.print(z+ " ");
}
System.out.println();
}
System.out.println();
}
*/
//Accessing three dimensional integer array elements using for loop
System.out.println("The output of three dimensional integer array is");
for(int i=0;i<intArray.length;i++) {
for(int j=0;j<intArray.length;j++) {
for(int z=0;z<intArray.length;z++) {
System.out.print(intArray[i][j][z]+" ");
}
System.out.println();
}
System.out.println();
}
}
}
Output:
The output of three-dimensional integer array is
1 2
3 4
3 3
4 4
2. Arrays.asList() method:
Arrays.asList() method is used to create a fixed-size array list which converts the arrays or custom class objects to the collection list-objects. asList() is a static method in java.util.Arrays class. import java.util.Arrays;
import java.util.List;
public class PrintArrayUsingArrays {
public static void main(String args[]) {
//Declare and initialize string array
String cars[]={"Ford","Benz","Polo","Audi"};
//Accessing and printing string array elements using Arrays.asList()
List <String> carList=Arrays.asList(cars);
System.out.println("The output of string array using Arrays asList() is"+
carList);
}
}
Output:
The output of string array using Arrays asList() is[Ford, Benz, Polo, Audi]
3. Using Arrays.toString() method
Arrays.toString() method is a static method in the Arrays class that takes arrays or custom class objects as input and returns the string representation of the specified object. import java.util.Arrays;
import java.util.List;
public class PrintArrayUsingArraysToStringMethod {
public static void main(String args[]) {
//Declare and initialize integer array
int intArray[]= {1,2,3,4,5};
//Accessing and printing elements using Arrays.toString()
String convertedStringArray=Arrays.toString(intArray);
System.out.println("The output of Arrays.toString() is"+"
"+convertedStringArray);
}
}
Output:
The output of Arrays.toString() is[1, 2, 3, 4, 5]
4. Using Arrays.deepToString() method
Arrays.deepToString() is a static method in the Arrays class that is used to convert and print multidimensional arrays to strings. import java.util.Arrays;
import java.util.List;
public class PrintArrayUsingArraysDeepString {
public static void main(String args[]) {
//Declare and initialize two dimensional integer array
int intArray[][]= {{11,11,11},{15,15,15},{19,19,19}};
//Accessing and printing elements using Arrays.deepString()
String convertedStringArray=Arrays.deepToString(intArray);
System.out.println("The output of Arrays.deepString() is"+" "+convertedStringArray);
}
}
Output:
The output of Arrays.deepString() is [[11, 11, 11], [15, 15, 15], [19, 19, 19]]
5. Using Iterator interface
In the below example, we used Iterator interface methods from java. util package to iterate over the array elements and print them. import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class PrintArrayUsingIterator {
public static void main(String args[]) {
//Declare and initialize an array of Integer objects.
Integer intArray[]= {100,101,102,103};
//Create a list of an Integer list object.
List<Integer> listOfIntegers=Arrays.asList(intArray);
/*Use iterator() method to create an iterator object and print array
elements.*/
Iterator<Integer> iteratorList=listOfIntegers.iterator();
System.out.print("The output is"+ " ");
while(iteratorList.hasNext()) {
System.out.print(iteratorList.next()+ " ");
}
}
}
Output:
The output is 100 101 102 103
6. Using Stream API
In the below example, we used Stream API to traverse the array objects and print them using the forEach method. import java.util.Arrays;
import java.util.List;
public class PrintArrayUsingStreamAPI {
public static void main(String args[]) {
//Declare and initialize string array
String flowers[]={"Lotus","Marigold","Rose","Hibiscus"};
//Access and print using Stream API
System.out.println("The output is");
Arrays.stream(flowers).forEach(System.out::println);
}
}
Output:
Lotus
Marigold
Rose
Hibiscus
That's all for today, please mention in the comments in case you have any questions related to how to print array in Java.