Table of Contents
TL;DR
Let’s say you have ArrayList namedcountryList
. Here are ways to print ArrayList in Java.
In case of inbuilt objects, you can use below methods.
1234 // Print ArrayList directlySystem.out.println("Elements of ArrayList are:"+countryList);Or
1234 // Using StreamscountryList.forEach(System.out::println);Or
1234 // Using StreamscountryList.stream().forEach(System.out::println);Or
12345678 // Using For each loop// Please change String to desired data typeSystem.out.println("Elements of ArrayList are:");for (String ele : countryList) {System.out.println(ele);}In case, you have custom object, you can directly jump to Print ArrayList of Custom objects section.
In this post, we will see how to print ArrayList in java.
Java has Array, and it’s a very powerful data structure, but Array has its own set of limitations. The ArrayList data structure was introduced to overcome these limitations.
Before we proceed with our topic, let me quickly brief you with ArrayList.
1. ArrayList Introduction
ArrayList is an advanced version of Array, as it allows us to implement resizable arrays. Apart from this, ArrayList is a part of Java collections, and it implements Java List
. Though ArrayList is the advanced version of array, ArrayList is less efficient than basic arrays in terms of time optimization.
Since we are not focusing on ArrayList Introduction, I have kept it straight and simple; if you are interested in learning more about ArrayList, you can refer ArrayList in java.
2. Ways to Print ArrayList in Java
Yes, you read it right! Fortunately, there isn’t a single way to print ArrayList elements. Though there are dozens of different ways, I’ll cover the three most popular ones in this guide.
2.1 Using for loop
In the for loop, we are iterating upto the size() of Arraylist. In each iteration, using the get() method of ArrayList, we are retrieving individual element. Finally, with the help of System.out.println statements, we will print those elements.
Have a look at the illustration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; import java.util.ArrayList; public class PrintArrayListMain { public static void main(String[] args) { ArrayList<String> arl = new ArrayList<>(); arl.add("Hyundai"); arl.add("Kia"); arl.add("Tesla"); arl.add("MG"); System.out.println("Elements of ArrayList are:"); for (int i = 0; i < arl.size(); i++) { System.out.println(arl.get(i) + " "); } } } |
Output:
Hyundai
Kia
Tesla
MG
2.2 Using for-each Loop
This method is almost identical to the previous one; the only difference here is that we will use the advanced loop method. So, instead of the basic for loop, we will implement its advanced version with some different attributes.
Refer to the illustration below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; import java.util.ArrayList; public class PrintArrayListMain { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("India"); arrayList.add("Australia"); arrayList.add("Sudan"); arrayList.add("Norway"); System.out.println("Elements of ArrayList are:"); for (String ele : arrayList) { System.out.println(ele); } } } |
Output:
India
Australia
Sudan
Norway
If you look at the above two methods, you will notice only a few noticeable differences, but in terms of working, both these methods are very different.
In the first method, for loop was retrieving the total size, and using that size, the index was evaluated, and later element was accessed. But in this method, every ArrayList element is directly assessed serially using a String variable.
💡 Did you know?
You can not modify ArrayList while iterating using for each loop, Otherwise it will throw Exception.
2.3 Using iterator framework
So far, we have used basic loop concepts to print the ArrayList element, but in this method, we will use the Iterators; these are a part of the java collection framework. In this method, we are going to use the following method of Interators
Iterator()
: it returns an iterator for the ArrayList.hasNext()
: Performs a check on the ArrayList to find whether next element is present or not.next()
: Accesses the elements of ArrayList
Besides these three methods, we will also use the While loop to iterate the element checking step.
Refer to the illustration below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.Iterator; public class PrintArrayListMain { public static void main(String args[]) { ArrayList<String> arList = new ArrayList<>(); arList.add("MacOS"); arList.add("Windows"); arList.add("ChromeOS"); arList.add("Linux"); arList.add("Ubuntu"); System.out.println("Elements of ArrayList are:"); Iterator<String> arItr = arList.iterator(); while(arItr.hasNext()) { String ele = arItr.next(); System.out.println(ele); } } } |
Output:
MacOS
Windows
ChromeOS
Linux
Ubuntu
If you don’t want to use While loop in the above program, you can use the for loop.
2.4 Using ListIterator
You can also use ListIterator
rather than Iterator
to print ArrayList in java.
With ListIterator, you can traverse in front and back directions.
Let’s see with the help of example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; public class PrintArrayListMain { public static void main(String args[]) { ArrayList<String> citiesList = new ArrayList<>(); citiesList.add("Delhi"); citiesList.add("Mumbai"); citiesList.add("Paris"); citiesList.add("Rome"); citiesList.add("New york"); System.out.println("Elements of ArrayList are:"); ListIterator<String> listItr = citiesList.listIterator(); while(listItr.hasNext()) { String ele = listItr.next(); System.out.println(ele); } System.out.println("Elements of ArrayList in reverse order are:"); ListIterator<String> listItr2 = citiesList.listIterator(citiesList.size()); while(listItr2.hasPrevious()) { String ele = listItr2.previous(); System.out.println(ele); } } } |
Output:
Delhi
Mumbai
Paris
Rome
New york
Elements of ArrayList in reverse order are:
New york
Rome
Paris
Mumbai
Delhi
2.5 Using Stream
You can use Java 8 Stream‘s foreach loop to print ArrayList in java.
Here is an simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; import java.util.ArrayList; public class PrintArrayListMain { public static void main(String args[]) { ArrayList<String> fruitList = new ArrayList<>(); fruitList.add("Apple"); fruitList.add("Banana"); fruitList.add("Grapes"); fruitList.add("Orange"); System.out.println("Elements of ArrayList are:"); fruitList.stream().forEach(System.out::println); } } |
Output:
Apple
Banana
Grapes
Orange
2.6 Using toString()
If you just want to print ArrayList on console, you can directly print it using its reference. It will call toString()
method internally of type of ArrayList(String
in this example).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; import java.util.ArrayList; public class PrintArrayListMain { public static void main(String args[]) { ArrayList<String> fruitList = new ArrayList<>(); fruitList.add("Apple"); fruitList.add("Banana"); fruitList.add("Grapes"); fruitList.add("Orange"); System.out.println("Elements of ArrayList are:"+fruitList); } } |
Output:
3. Print ArrayList of Custom Objects
If you have custom object in ArrayList, then you may need to implement toString()
method in the custom object to print ArrayList properly.
Let’s see with the help of example:
Create a class named Student
with name
and age
attributes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.arpit.java2blog; public class Student { String name; int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
Create main class PrintArrayListStudentMain.java
which we will use to print the ArrayList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; import java.util.ArrayList; public class PrintArrayListStudentMain { public static void main(String[] args) { ArrayList<Student> studentsList=new ArrayList<>(); Student s1=new Student("John",12); Student s2=new Student("Martin",11); Student s3=new Student("Mary",10); Student s4=new Student("Sneha",13); studentsList.add(s1); studentsList.add(s2); studentsList.add(s3); studentsList.add(s4); System.out.println("Student List:"+studentsList); } } |
Output:
If you notice the output, it is printing unreadble String because we did not implement toString() method in Student class.
Let’s implement toString()
method in Student class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package org.arpit.java2blog.entry; public class Student { String name; int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } |
Now, run PrintArrayListStudentMain.java
again and you will get below output:
As you can see, we are able to print ArrayList
Conclusion
That’s how you print ArrayList elements using these three different methods quite easily. Apart from these three methods, you can come up with your method, do let us know. If you run into some errors, you can drop down your queries and we will discuss them further.
That’s all about how to print ArrayList in java.