- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate through an ArrayList using a ListIterator in Java
A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in an ArrayList.
The method hasNext( ) in ListIterator returns true if there are more elements in the ArrayList while traversing in the forward direction and false otherwise. The method next( ) returns the next element in the ArrayList and advances the cursor position.
The method hasPrevious( ) in ListIterator returns true if there are more elements in the ArrayList while traversing in the reverse direction and false otherwise. The method previous( ) returns the previous element in the ArrayList and reduces the cursor position backward.
A program that demonstrates this is given as follows.
Example
import java.util.ArrayList; import java.util.ListIterator; public class Demo { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); aList.add("Apple"); aList.add("Mango"); aList.add("Guava"); aList.add("Orange"); aList.add("Peach"); ListIterator i = aList.listIterator(); System.out.println("The ArrayList elements in the forward direction are: "); while (i.hasNext()) { System.out.println(i.next()); } System.out.println("
The ArrayList elements in the reverse direction are: "); while (i.hasPrevious()) { System.out.println(i.previous()); } } }
Output
The output of the above program is as follows
The ArrayList elements in the forward direction are
Apple Mango Guava Orange Peach The ArrayList elements in the reverse direction are: Peach Orange Guava Mango Apple
- Related Articles
- Iterate through elements of a LinkedList using a ListIterator in Java
- Iterate through a LinkedList in reverse direction using a ListIterator in Java
- Iterate through ArrayList in Java
- Replace an element in an ArrayList using the ListIterator in Java
- Remove an element from an ArrayList using the ListIterator in Java
- Iterate through a Collection using an Iterator in Java
- Iterate through a LinkedList using an Iterator in Java
- Loop through an ArrayList using an Iterator in Java
- Loop through the Vector elements using a ListIterator in Java
- Java Program to Iterate over an ArrayList
- Obtain the Previous Index and Next Index in an ArrayList using the ListIterator in Java
- Use ListIterator to traverse an ArrayList in the forward direction in Java
- Use ListIterator to traverse an ArrayList in the reverse direction in Java
- Java Program to Iterate over ArrayList using Lambda Expression
- Replace an element from a Java List using ListIterator
