Java ArrayList Traversal

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 70:-  Java ArrayList Traversal

In Java, there are several ways to traverse or iterate over the elements of an ArrayList. Traversal allows you to access and process each element in the list. Here are some common methods for traversing an ArrayList:

  1. Using Enhanced For Loop (For-each Loop): The enhanced for loop (for-each loop) simplifies the process of iterating over the elements in the ArrayList. It is especially useful when you only need to access the elements and do not require the index.

    javaCopy code

    import java.util.ArrayList; public class ArrayListTraversal {    public static void main(String[] args) {        ArrayList<String> fruits = new ArrayList<>();        fruits.add("Apple");        fruits.add("Banana");        fruits.add("Orange");        // Enhanced for loop (for-each loop)        for (String fruit : fruits) {            System.out.println(fruit);        }    } }

  2. Using Traditional For Loop: You can use a traditional for loop to iterate over the elements of the ArrayList. This method provides access to both the index and the elements.

    javaCopy code

    import java.util.ArrayList; public class ArrayListTraversal {    public static void main(String[] args) {        ArrayList<String> fruits = new ArrayList<>();        fruits.add("Apple");        fruits.add("Banana");        fruits.add("Orange");        // Traditional for loop        for (int i = 0; i < fruits.size(); i++) {            String fruit = fruits.get(i);            System.out.println(fruit);        }    } }

  3. Using Iterator: You can use an iterator to traverse through the elements of the ArrayList. The iterator is part of the Java Collections Framework and provides a safe way to access and modify elements during traversal.

    javaCopy code

    import java.util.ArrayList; import java.util.Iterator; public class ArrayListTraversal {    public static void main(String[] args) {        ArrayList<String> fruits = new ArrayList<>();        fruits.add("Apple");        fruits.add("Banana");        fruits.add("Orange");        // Using iterator        Iterator<String> iterator = fruits.iterator();        while (iterator.hasNext()) {            String fruit = iterator.next();            System.out.println(fruit);        }    } }

  4. Using forEach Method (Java 8+): If you are using Java 8 or later, you can use the forEach method introduced in the Iterable interface to iterate over the elements of the ArrayList.

    javaCopy code

    import java.util.ArrayList; public class ArrayListTraversal {    public static void main(String[] args) {        ArrayList<String> fruits = new ArrayList<>();        fruits.add("Apple");        fruits.add("Banana");        fruits.add("Orange");        // Using forEach method (Java 8+)        fruits.forEach(fruit -> System.out.println(fruit));    } }

All of the above methods will produce the same output, printing each fruit on a separate line:

mathematicaCopy code

Apple Banana Orange

You can choose the method that best suits your requirements and coding style when traversing an ArrayList.

11. ArrayList

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?