Iterating over ArrayLists in Java

Prerequisite : ArrayList in Java

Method 1 : Using for loop :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java  program to iterate over an ArrayList
// using for loop
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
  
        // initializing ArrayList
        List<Integer> numbers = Arrays.asList(1, 2, 3,
                                       4, 5, 6, 7, 8);
  
        // For Loop for iterating ArrayList
        for (int i = 0; i < numbers.size(); i++) 
            System.out.print(numbers.get(i) + " ");        
    }
}

chevron_right


Output:

1 2 3 4 5 6 7 8

Method 2: Using for each loop

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to iterate
// over an arraylist using for Each loop
import java.util.*;
class GFG {
  
    public static void main(String[] args)
    {
        // initializing ArrayList
        List<Integer> numbers = Arrays.asList(1, 2, 3,
                                      4, 5, 6, 7, 8);
  
        // For Each Loop for iterating ArrayList
        for (Integer i : numbers)
            System.out.print(i + " ");
    }
}

chevron_right


Output:



1 2 3 4 5 6 7 8

Method 3 : Using Iterator

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to iterate over an arraylist
// using Iterator
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // initializing ArrayList
        List<Integer> numbers = Arrays.asList(1, 2, 3
                                       4, 5, 6, 7, 8);
  
        // Looping ArrayList using Iterator
        Iterator it = numbers.iterator();
        while (it.hasNext())
            System.out.print(it.next() + " ");
    }
}

chevron_right


Output:

1 2 3 4 5 6 7 8

Method 3 : Using For Each Method of Java 8

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to iterate over an arraylist
// using Iterator
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // initializing ArrayList
        List<Integer> numbers = Arrays.asList(1, 2, 3
                                       4, 5, 6, 7, 8);
  
           //lambda
        numbers.forEach(number->System.out.println(number));
  
    }
}

chevron_right


Output:

1 2 3 4 5 6 7 8


Removing Items during Traversal :

It is not recommended to use ArrayList.remove() when iterating over elements. This may lead to ConcurrentModificationException (Refer this for a sample program with this exception). When iterating over elements, it is recommended to use Iterator.remove() method .

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of
// Iterator.remove() on an integer arraylist
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
  
public class GFG
{
    public static void main(String[] args)
    {
        List<Integer> al = new ArrayList<Integer>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(1);
        al.add(2);
  
        // Remove elements smaller than 10 using
        // Iterator.remove()
        Iterator itr = al.iterator();
        while (itr.hasNext())
        {
            int x = (Integer)itr.next();
            if (x < 10)
                itr.remove();
        }
  
        System.out.println("Modified ArrayList : "
                                           + al);
    }
}

chevron_right


This article is contributed by Nikita Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Improved By : SandeepChaudhary

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.