Iterating over ArrayLists in JavaArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. Show With the introduction and upgradations in java versions, newer methods are being available as if we do see from Java8 perceptive lambda expressions and streams concepts were not available before it as it been introduced in java version8. Methods:
Let us discuss these methods of which straight away we can perceive starting three methods are simply the naive approaches and further onwards methods carry some optimization with them. Do remember here while traversing elements are lesser we generally tend to iterate via naive approach only else if the size of elements to be inserted is big then we do use optimal approaches. Let us wrap each of the above approaches quickly. Method 1: Using for loop Java
Output
1 2 3 4 5 6 7 8
Method 2: Using while loop Java
Output
3
1
7
20
5
Method 3: Using for each loop Java
Output
1 2 3 4 5 6 7 8
Method 4: Using Iterator Java
Output
1 2 3 4 5 6 7 8
Method 5: Using Lambda expressions Java
Output
1
2
3
4
5
6
7
8
Method 6: Using Enumeration interface Java
Output
34
12
34
23
54
Now it is a further additive to the article as we are done with discussing all methods that can be used to iterate over elements. Till now we have traversed over input elements only and have not seen the traversal what if we play with elements, so do we are considering Example Java
Output
Modified ArrayList : [10, 20, 30]
Removing Items during Traversal: It is not recommended to use ArrayList.remove() when iterating over elements. This may lead to ConcurrentModificationException (Refer to this for a sample program with this exception). When iterating over elements, it is recommended to use Iterator.remove() method. This article is contributed by Nikita Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Article Tags :
Java
Java-ArrayList Java-List-Programs Practice Tags :
Java ArrayList iterator() method in Java with ExamplesThe iterator() method of ArrayList class in Java Collection Framework is used to get an iterator over the elements in this list in proper sequence. The returned iterator is fail-fast. Syntax: Iterator iterator()Parameter: This method do not accept any parameter. Return Value: This method returns an iterator over the elements in this list in proper sequence Below examples illustrate the ArrayList.iterator() method: Program 1:
Output:
The list is:
[Geeks, for, Geeks, is, a, CS, Students, Portal]
The iterator values of list are:
Geeks for Geeks is a CS Students Portal
Program 2:
Output:
The list is:
[10, 15, 30, 20, 5]
The iterator values of list are:
10 15 30 20 5
Article Tags :
Java Technical Scripter
Java-ArrayList Java-Collections Java-Functions Technical Scripter 2018 Practice Tags :
Java Java-Collections 1. Iterate ArrayList with ‘for loop’Java program to iterate through an ArrayList of objects using the standard for loop. ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles") ); for(int i = 0; i < namesList.size(); i++) { System.out.println(namesList.get(i)); }2. Iterate ArrayList with ‘for-each loop’Java program to iterate through an ArrayList of objects using for-each loop. ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles") ); for(String name : namesList) { System.out.println(name); }Java Program to Iterate over an ArrayListIn this example, we will learn to iterate over the elements of an arraylist in Java. To understand this example, you should have the knowledge of the following Java programming topics:
Different Ways Of Iterating An ArrayList In Java :You can iterate a given ArrayList in 4 different ways. They are, a) Iteration Using Normal for loop. b) Iteration Using Iterator Object. c) Iteration Using ListIterator Object. d) Iteration Using Enhanced for loop. Below is thedetaildescription of all of the above methods. |