Traverse Through ArrayList in Forward Direction in Java
ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java. The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in a proper organized sequence). ArrayList can be traversed in the forward direction using multiple ways.
Example:
Input : ArrayList: [5, 6, 8, 10] Output: Value is : 5 Value is : 6 Value is : 8 Value is : 10
Approach 1: Using listIterator Method
- Create a list iterator object of a given ArrayList.
- Use while loop with the condition as hasNext() method.
- If hasNext() method returns false, loop breaks.
- Else print the value using object.next() method.
Example:
// Traverse through ArrayList in
// forward direction using Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> alist = new ArrayList<>();
// adding element to arrlist
alist.add(5);
alist.add(6);
alist.add(8);
alist.add(10);
ListIterator<Integer> it = alist.listIterator();
while (it.hasNext()) {
System.out.println("Value is : " + it.next());
}
}
}
// Traverse through ArrayList in
// forward direction using Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> alist = new ArrayList<>();
// adding element to arrlist
alist.add(5);
alist.add(6);
alist.add(8);
alist.add(10);
ListIterator<Integer> it = alist.listIterator();
while (it.hasNext()) {
System.out.println("Value is : " + it.next());
}
}
}
Output
Value is : 5 Value is : 6 Value is : 8 Value is : 10
Time Complexity: O(N), where N is the length of ArrayList.
Approach 2: Using For Loop
Print all the values in ArrayList using for loop. Size of ArrayList can be obtained using ArrayList.size() method and for accessing the element use ArrayList.get() method.
Implementation:
// Traverse through ArrayList in
// forward direction using Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> alist = new ArrayList<>();
// adding element to arrlist
alist.add(5);
alist.add(6);
alist.add(8);
alist.add(10);
for (int i = 0; i < alist.size(); i++)
System.out.println("Value is : "
+ alist.get(i));
}
}
// Traverse through ArrayList in
// forward direction using Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> alist = new ArrayList<>();
// adding element to arrlist
alist.add(5);
alist.add(6);
alist.add(8);
alist.add(10);
for (int i = 0; i < alist.size(); i++)
System.out.println("Value is : "
+ alist.get(i));
}
}
Output
Value is : 5 Value is : 6 Value is : 8 Value is : 10
Time Complexity: O(N), where N is the length of ArrayList.
Approach 3: Using forEach Loop
Print all the values in ArrayList using for each loop.
Example:
// Traverse through ArrayList in
// forward direction using Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> alist = new ArrayList<>();
// adding element to arrlist
alist.add(5);
alist.add(6);
alist.add(8);
alist.add(10);
for (Integer i : alist)
System.out.println("Value is : " + i);
}
}
// Traverse through ArrayList in
// forward direction using Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> alist = new ArrayList<>();
// adding element to arrlist
alist.add(5);
alist.add(6);
alist.add(8);
alist.add(10);
for (Integer i : alist)
System.out.println("Value is : " + i);
}
}
Output
Value is : 5 Value is : 6 Value is : 8 Value is : 10
Time Complexity: O(N), where N is the length of ArrayList.
Approach 4: Using Lambda Function
Print all the values in ArrayList using the lambda function.
Example:
// Traverse through ArrayList in
// forward direction using Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> alist = new ArrayList<>();
// adding element to arrlist
alist.add(5);
alist.add(6);
alist.add(8);
alist.add(10);
// lambda
alist.forEach(
i -> System.out.println("Value is : " + i));
}
}
// Traverse through ArrayList in
// forward direction using Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> alist = new ArrayList<>();
// adding element to arrlist
alist.add(5);
alist.add(6);
alist.add(8);
alist.add(10);
// lambda
alist.forEach(
i -> System.out.println("Value is : " + i));
}
}
Output
Value is : 5 Value is : 6 Value is : 8 Value is : 10
Time Complexity: O(N), where N is the length of ArrayList.