"5 ways to iterate through ArrayList in Java"

🧭 Day 16: Iterating Through ArrayList in Java Today, I learned different ways to loop through an ArrayList — each method gives flexibility based on what you need. 💡 What I Learned Today For Loop – Access elements using index. Enhanced For Loop (for-each) – Simple and clean way to read data. Iterator – Best when you want to remove elements while looping. ListIterator – Can iterate both forward and backward. forEach() Method – Uses lambda expressions for modern Java style. 🧩 Example Code import java.util.*; public class IterateArrayList {   public static void main(String[] args) {     ArrayList<String> names = new ArrayList<>();     names.add("Raj");     names.add("Arun");     names.add("Kumar");     // For loop     for (int i = 0; i < names.size(); i++)       System.out.println("For Loop: " + names.get(i));     // For-each loop     for (String name : names)       System.out.println("For-each: " + name);     // Iterator     Iterator<String> it = names.iterator();     while (it.hasNext())       System.out.println("Iterator: " + it.next());     // forEach method     names.forEach(n -> System.out.println("Lambda: " + n));   } } 🗣️ LinkedIn Caption 🔁 Day 16 – Iterating through ArrayList in Java Today I explored 5 powerful ways to loop through ArrayLists — from classic for-loops to modern lambdas. Each method offers unique control and flexibility when working with lists. #Java #CoreJava #ArrayList #LearnJava #Programming

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories