Here Is 4 Ways To Print ArrayList Elements In Java

Learn 4 Techniques to PRINT ArrayList Elements in Java with Code Example.

To print elements, first we’ll create a String ArrayList and store weekdays name as strings into it and display them using following ways:

  1. For-loop
  2. For-each loop
  3. Using iterator
  4. Using List-iterator

Here is a string ArrayList. We have created an object of the list and added name of week days using add() method of ArrayList.

ArrayList arrlist=new ArrayList();
		arrlist.add("Sunday");
		arrlist.add("Monday");
		arrlist.add("Tuesday");
		arrlist.add("Wednesday");
		arrlist.add("Thursday");
		arrlist.add("Friday");
		arrlist.add("Saturday");

PRINTING ARRAYLIST

1) Using for loop

//using for loop

                   System.out.println("Using For Loop\n ");
	      for (int i = 0; i < arrlist.size();i++) 
	      { 		      
	          System.out.println(arrlist.get(i)); 		
	      }   

To use for loop, we’re getting the length of the ArrayList using its size() method, up to which we need to retrieve elements. We have used get(i) method of the arraylist to fetch the data present at the indexes of the arraylist.

2) Using for-each loop

//using for-each loop
	      System.out.println("\nUsing for-each loop\n");		
	      for (String str : arrlist)
	      { 		      
	           System.out.println(str); 		
	      }

Here, the same for loop is written in another form using for each loop or advance loop method in java. This type of loop fetchs every elements from the arralist object one by one.

FYI, MY BOOK FOR YOU

3) Using iterator

//using iterator
	      System.out.println("\nUsing Iterator");
	      Iterator itr=arrlist.iterator();
	      while(itr.hasNext())
	        {
	          String obj = itr.next();
	          System.out.println(obj);
	        }

Iterators in java collection framework are used to retrieve elements one by one. ArrayList iterator() method returns an iterator for the list. The Iterator contains methods hasNext() that checks if next element is available. Another method next() of Iterator returns elements.

We have implemented while loop to traverse the ArrayList.  In the loop, we are checking if next element is available using hasNext() method. Iterator hasNext() methods returns the Boolean value  true or false. If hasNext() returns true, the loop fetchs the ArrayList elements using Iterator next() method and print it using System.out.println mehtod. If hasNext() returns false, there is no further elements in the list.

4) Using list-iterator

//Using list iterator
	      litr=arrlist.listIterator();
	   
	      System.out.println("\n Using list iterator");
	      while(litr.hasNext()){
	         System.out.println(litr.next());
	      }

We can also use ListIterator to traverse elements of the ArrayList. ListIterator also has methods hasNext() to check if element is present and next() method to retrieve ArrayList elements. ListIterator is similar to iterator with difference that iterator is unidirectional and ListIterator is bidirectional.

The only thing in case of list iterator is that we have to initialize it with null at first.

ListIterator<String> litr = null;

Complete Code to print ArrayList Elements in Java using all 4 Ways

The complete code to print all elements of the ArrayList using all 4 techniques is given below:

import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
public class Arrlist {

	public static void main(String[] args) {
	
		 ListIterator litr = null;
		ArrayList arrlist=new ArrayList();
		arrlist.add("Sunday");
		arrlist.add("Monday");
		arrlist.add("Tuesday");
		arrlist.add("Wednesday");
		arrlist.add("Thursday");
		arrlist.add("Friday");
		arrlist.add("Saturday");
		
		//using for loop 
		System.out.println("Using For Loop\n ");
	      for (int i = 0; i < arrlist.size();i++) 
	      { 		      
	          System.out.println(arrlist.get(i)); 		
	      }   		
		
		//using for-each loop
	      System.out.println("\nUsing for-each loop\n");		
	      for (String str : arrlist)
	      { 		      
	           System.out.println(str); 		
	      }

	      //using iterator
	      System.out.println("\nUsing Iterator");
	      Iterator itr=arrlist.iterator();
	      while(itr.hasNext())
	        {
	          String obj = itr.next();
	          System.out.println(obj);
	        }
	    //Using list iterator
	      litr=arrlist.listIterator();
	   
	      System.out.println("\n Using list iterator");
	      while(litr.hasNext()){
	         System.out.println(litr.next());
	      }
	      
	}

}

Output
Using For Loop

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Using for-each loop

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Using Iterator
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Using list iterator
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Users Question on ArrayList from comments:

Q – Suppose I have three or more arraylist . Do I need to write the code again and again to print the Elements ? – Himanshu

Answer: NO, you can write a single method to print multiple arraylists and call the method for each array list as shown below.

In the below code example, there are two array list “days” and “fruits” . The single method “printArrayListElements(ArrayList a)” is being used to print both the array lists.

import java.util.ArrayList;
public class Sample {

	public static void main(String[] args) {

		ArrayList days = new ArrayList();
		days.add("Sunday");
		days.add("Monday");
		days.add("Tuesday");
		days.add("Wednesday");
		days.add("Thursday");
		days.add("Friday");
		days.add("Saturday");

		System.out.println("Days Array List:");
		printArrayListElements(days);

		ArrayList fruits = new ArrayList();
		fruits.add("Apple");
		fruits.add("Mango");
		fruits.add("Orange");

		System.out.println("Fruits Array List:");
		// print fruits array list
		printArrayListElements(fruits);

	}

	public static void printArrayListElements(ArrayList a) {
		for (int i = 0; i < a.size(); i++) {
			System.out.println(a.get(i));
		}
	}
}

Output:

Days Array List:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Fruits Array List:
Apple
Mango
Orange

Related Posts