Iterate arraylist in java: In this article, we will see how to iterate an ArrayList in Java programming language.
Java Program to Iterate an ArrayList
Java iterate arraylist: We can iterate an ArrayList by different ways. Let’s see one by one.
Method-1: Java Program to Iterate an ArrayList By Using ForEach Loop
Approach:
- Create a String ArrayList say
list
and add elements into it. - Traverse the list using
ForEach
Loop and print the elements one by one.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Creating String ArrayList ArrayList<String> list=new ArrayList<String>(); // Adding elements into the list list.add("Queen"); list.add("King"); list.add("Ace"); list.add("Joker"); //Traversing list through for-each loop System.out.println("Cards in the list are :"); for(String cards:list) { System.out.println(cards); } } }
Output: Cards in the list are : Queen King Ace Joker
Method-2: Java Program to Iterate an ArrayList By Using For Loop
Approach:
- Create a String ArrayList say
list
and add elements into it. - Traverse the list using
For
Loop and print the elements one by one.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Creating String ArrayList ArrayList<String> list=new ArrayList<String>(); // Adding elements into the list list.add("Queen"); list.add("King"); list.add("Ace"); list.add("Joker"); //Traversing list through for loop System.out.println("Cards in the list are :"); for (int i = 0; i< list.size();i++) { System.out.println(list.get(i)); } } }
Output: Cards in the list are : Queen King Ace Joker
Method-3: Java Program to Iterate an ArrayList By Using While Loop
Approach:
- Create a String ArrayList say
list
and add elements into it. - Traverse the list using
While
Loop and print the elements one by one.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Creating String ArrayList ArrayList<String> list=new ArrayList<String>(); // Adding elements into the list list.add("Queen"); list.add("King"); list.add("Ace"); list.add("Joker"); //Traversing list through while loop System.out.println("Cards in the list are :"); int items = 0; while (items < list.size()) { System.out.println(list.get(items)); items++; } } }
Output: Cards in the list are : Queen King Ace Joker
Method-4: Java Program to Iterate an ArrayList By Using Iterator Loop
Approach:
- Create a String ArrayList say
list
and add elements into it. - Traverse the list using
iterator
and print the elements one by one.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Creating String ArrayList ArrayList<String> list=new ArrayList<String>(); // Adding elements into the list list.add("Queen"); list.add("King"); list.add("Ace"); list.add("Joker"); //Traversing list through iterator loop System.out.println("Cards in the list are :"); Iterator iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }
Output: Cards in the list are : Queen King Ace Joker
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.