ArrayList 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.
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:
- Using for loops
- Using while
- Using for-each loop
- Using Iterator
- Using Lambda expressions (after Java8 only)
- Using Enumeration interface
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> numbers = Arrays.asList( 1 , 2 , 3 ,
4 , 5 , 6 , 7 , 8 );
for ( int i = 0 ; i < numbers.size(); i++)
System.out.print(numbers.get(i) + " " );
}
}
|
Method 2: Using while loop
Java
import java.util.ArrayList ;
public class GFG {
public static void main(String[] args)
{
ArrayList<Integer> al = new ArrayList<Integer>();
al.add( 3 );
al.add( 1 );
al.add( 7 );
al.add( 20 );
al.add( 5 );
int val = 0 ;
while (al.size() > val) {
System.out.println(al.get(val));
val++ ;
}
}
}
|
Method 3: Using for each loop
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> numbers
= Arrays.asList( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 );
for (Integer i : numbers)
System.out.print(i + " " );
}
}
|
Method 4: Using Iterator
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> numbers
= Arrays.asList( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 );
Iterator it = numbers.iterator();
while (it.hasNext())
System.out.print(it.next() + " " );
}
}
|
Method 5: Using Lambda expressions
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> numbers = Arrays.asList( 1 , 2 , 3 ,
4 , 5 , 6 , 7 , 8 );
numbers.forEach(number->System.out.println(number));
}
}
|
Method 6: Using Enumeration interface
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> al = new ArrayList<Integer>();
al.add( 34 );
al.add( 12 );
al.add( 34 );
al.add( 23 );
al.add( 54 );
Enumeration<Integer> e
= Collections.enumeration(al);
while (e.hasMoreElements())
System.out.println(e.nextElement());
}
}
|
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
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 );
Iterator itr = al.iterator();
while (itr.hasNext())
{
int x = (Integer)itr.next();
if (x < 10 )
itr.remove();
}
System.out.println( "Modified ArrayList : "
+ al);
}
}
|
OutputModified 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 review-team@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.