🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
ArrayList.listIterator() method in Java is used to obtain a list iterator for the elements in an ArrayList. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
listIteratorMethod Syntax- Examples
- Using
listIterator() - Using
listIterator(int index)
- Using
- Conclusion
Introduction
The ArrayList.listIterator() method is a member of the ArrayList class in Java. It returns a list iterator over the elements in the ArrayList in proper sequence. This method is particularly useful for traversing the list, performing operations on its elements, and iterating both forwards and backwards.
listIterator Method Syntax
There are two overloaded versions of the listIterator method:
1. listIterator()
public ListIterator<E> listIterator()
- Returns a list iterator over the elements in the
ArrayListin proper sequence, starting at the beginning of the list.
2. listIterator(int index)
public ListIterator<E> listIterator(int index)
- index: The index of the first element to be returned from the list iterator.
- Returns a list iterator over the elements in the
ArrayListin proper sequence, starting at the specified position in the list.
Examples
Using listIterator()
The listIterator() method can be used to obtain a list iterator that starts at the beginning of the ArrayList.
Example
import java.util.ArrayList;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Obtain a list iterator
ListIterator<String> iterator = list.listIterator();
// Iterate forwards
System.out.println("Iterating forwards:");
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
// Iterate backwards
System.out.println("Iterating backwards:");
while (iterator.hasPrevious()) {
String element = iterator.previous();
System.out.println(element);
}
}
}
Output:
Iterating forwards:
Apple
Banana
Orange
Iterating backwards:
Orange
Banana
Apple
Using listIterator(int index)
The listIterator(int index) method can be used to obtain a list iterator that starts at a specified position in the ArrayList.
Example
import java.util.ArrayList;
import java.util.ListIterator;
public class ListIteratorWithIndexExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Obtain a list iterator starting at index 1
ListIterator<String> iterator = list.listIterator(1);
// Iterate forwards from index 1
System.out.println("Iterating forwards from index 1:");
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
// Iterate backwards from the end of the list
System.out.println("Iterating backwards:");
while (iterator.hasPrevious()) {
String element = iterator.previous();
System.out.println(element);
}
}
}
Output:
Iterating forwards from index 1:
Banana
Orange
Iterating backwards:
Orange
Banana
Apple
Conclusion
The ArrayList.listIterator() method in Java provides a way to traverse and manipulate the elements of an ArrayList using a list iterator. By understanding how to use both versions of this method, you can efficiently iterate over elements in either direction and perform operations on them in your Java applications. The listIterator method offers a flexible and powerful solution for these tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment