Why is there a Need to Print Arrays in Java?
Java provides Array data structure to store different elements of the same data type. The elements are stored in contiguous memory. To display the similar contents of the array, the elements are needed to be printed.Methods to Print an Array in Java
There are a bunch of different ways to print an array in Java. You can use manual traversals using for loops or opt for any standard library methods to do the same. Here is a list of ways to print arrays in Java that we will be exploring in this article.- for loop
- for each loop
- Arrays.toString() method
- Arrays.toList() method
- Java Iterators
Method I - Printing array using for loop
This is the simplest way, to begin with. Here’s how you can do it.
public class printArrayMethod1 {
public static void main(String[] args) {
String[] monthsOfTheYear = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" };
System.out.println("Months of the year are as follows:");
// Method I - Printing array using for loop
for (int i = 0; i < monthsOfTheYear.length; i++) {
System.out.println(monthsOfTheYear[i]);
}
}
}
Output
Months of the year are as follows:
January
February
March
April
May
June
July
August
September
October
November
December
Method II - Printing array using for each loop
For each loop is another form of the basic for loop. Here you do not need to initialize and increment the loop iterator. The loop directly traverses the elements of the array. Making it simpler to use.
public class printArrayMethod2 {
public static void main(String[] args) {
String[] monthsOfTheYear = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" };
System.out.println("Months of the year are as follows:");
// Method II - Printing array using for each loop
for (String month : monthsOfTheYear) {
System.out.println(month);
}
}
}
Output
Months of the year are as follows:
January
February
March
April
May
June
July
August
September
October
November
December
Method III - Using Standard Library Arrays
The Java Arrays.toString() method is provided by the java.util.Arrays class. It takes an array as an input parameter. The array can be of any primitive type. Later, the array is converted to a String before printing on the console.
import java.util.Arrays;
public class printArrayMethod3 {
public static void main(String[] args) {
String[] monthsOfTheYear = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" };
System.out.println("Months of the year are as follows:");
// Method III - Using Standard Library Arrays
System.out.println(Arrays.toString(monthsOfTheYear));
}
}
Output
As you can see in the output, the whole contiguous array elements are printed comma-separated on the console.Months of the year are as follows:
[January, February, March, April, May, June, July, August, September, October, November, December]
Method IV - Using Standard Library Arrays asList Method
The Java Arrays.asList() method is also provided by the java.util.Arrays class. A primitive data type array can be passed to it as a parameter. Later, the list type view of the input array is printed on the console.
import java.util.Arrays;
public class printArrayMethod4 {
public static void main(String[] args) {
String[] monthsOfTheYear = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" };
System.out.println("Months of the year are as follows:");
// Method IV - Using Standard Library Arrays asList Method
System.out.println(Arrays.asList(monthsOfTheYear));
}
}
Output
Months of the year are as follows:
[January, February, March, April, May, June, July, August, September, October, November, December]
Method V - Using Iterators to traverse the Array
This is a little advanced method. You may like to get acquainted with the Collections Framework in Java before proceeding. Java provides an interface called “iterator” present in java.util package. Iterator object is used to traverse over the objects of the Collection class. Therefore, in the following example, the array needs to be converted to a “List” before using the iterator.
import java.util.Arrays;
import java.util.Iterator;
public class printArrayMethod5 {
public static void main(String[] args) {
String[] monthsOfTheYear = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" };
System.out.println("Months of the year are as follows:");
// Method V - Using Iterators to traverse the Array
Iterator<String> itr = Arrays.asList(monthsOfTheYear).iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
Months of the year are as follows:
January
February
March
April
May
June
July
August
September
October
November
December
GO TO FULL VERSION