Home > Java > javaTutorial > body text

Java List Iterator

WBOY
Release: 2024-08-30 15:09:36
Original
1072 people have browsed it

In Java, ListIterator is a bidirectional iterator that helps to iterate elements in a list one-by-one. It is available from version Java 1.2 and extends the interface Iterator. Unlike normal iterator, ListIterator supports CRUD operations, forward and backward iterations. Furthermore, as the position of the cursor is in between elements that will be returned on calling previous() and element that will be returned on calling next (), listiterator does not have a current element.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

Below is the syntax of Java ListIterator.

ListIterator<T> li = list.listIterator();
Copy after login

How does the ListIterator classwork in Java?

As already discussed, Java ListIterator is a Bi-directional Iterator that traverses in the forward direction and backward direction. For supporting this functionality, two sets of methods are available.

  • Forward Direction methods such as hasNext(), next(), nextIndex()
  • Backward Direction methods such as hasPrevious(), previous(), previousIndex()

These methods will be discussed in the next section in detail.

Let us see how ListIterator works.

1. Create a list of names

List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Elsa");
nm.add("Anna");
nm.add("payal");
Copy after login

2. Create a ListIterator object

//get list iterator
ListIterator<String> li =nm.listIterator(); In the beginning, the Cursor of ListIterator will be pointed before the first element in the List. On calling the methods li.hasNext() and li.next() in a while loop, the cursor will be moved to the last element in the list. In order to start traversing from last, li.hasPrevious( ) and li.previous() methods will be called. On reaching the first element and again calling the same method will return a false value.
Copy after login

Methods of Java Listiterator

The following are the different methods used in Java ListIterator.

1. add(E elm)

Element elm will be inserted into the list. Code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
public static void main(String a[])
{
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
//add the elements to the list
nm.add("Ram");
nm.add("Jaanu");
nm.add("Elsa");
nm.add("Anna");
nm.add("payal");
//get list iterator
li=nm.listIterator();
System.out.println("List is :");
while(li.hasNext()){
System.out.println(li.next());
}
}
}
Copy after login

Output:

Java List Iterator

2. hasNext()

True will be returned if the iterator has the next elements during the traversal in the forward direction.

Code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
public static void main(String a[])
{
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
//get list iterator
li=nm.listIterator();
System.out.println("List is :");
//iterates if next element is present in forward direction
while(li.hasNext()){
System.out.println(li.next());
}
}
}
Copy after login
Copy after login

Output:

Java List Iterator

3. hasPrevious()

True will be returned if the iterator has the next elements during the traversal in a backward direction.

Code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
//get list iterator
li=nm.listIterator();
System.out.println("Traversal in forward direction::");
while(li.hasNext()){
System.out.println(li.next());
}
System.out.println("\nTraversal in backward direction:");
while(li.hasPrevious()){
System.out.println(li.previous());
}
}
}
Copy after login
Copy after login

Output:

Java List Iterator

4. next()

The next element will be returned, and the cursor position will be advanced.

Code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
public static void main(String a[])
{
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
//get list iterator
li=nm.listIterator();
System.out.println("List is :");
//iterates if next element is present in forward direction
while(li.hasNext()){
System.out.println(li.next());
}
}
}
Copy after login
Copy after login

Output:

Java List Iterator

5. nextIndex()

The index will be returned for the element that will be returned on calling next().

Code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Khan");
nm.add("Alia");
nm.add("Jhanvi");
//get list iterator
li=nm.listIterator();
System.out.println("The index of first element : " + li.nextIndex());
li.next();
System.out.println("The index of second element :  " + li.nextIndex());
li.next();
System.out.println("The index of third element : " + li.nextIndex());
li.next();
System.out.println("The index of fourth element : " + li.nextIndex());
li.next();
System.out.println("The index of fifth element : " + li.nextIndex());
}
}
Copy after login

Output:

Java List Iterator

6. previous()

The previous element will be returned, and the cursor position will be moved backward.

Code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
//get list iterator
li=nm.listIterator();
System.out.println("Traversal in forward direction::");
while(li.hasNext()){
System.out.println(li.next());
}
System.out.println("\nTraversal in backward direction:");
while(li.hasPrevious()){
System.out.println(li.previous());
}
}
}
Copy after login
Copy after login

Output:

Java List Iterator

7. previousIndex()

The index will be returned for the element that will be returned on calling previous().

Code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Khan");
nm.add("Alia");
nm.add("Jhanvi");
//get list iterator
li=nm.listIterator();
System.out.println("The index of first element : " + li.previousIndex());
li.next();
System.out.println("The index of second element :  " + li.previousIndex());
li.next();
System.out.println("The index of third element : " + li.previousIndex());
li.next();
System.out.println("The index of fourth element : " + li.previousIndex());
li.next();
System.out.println("The index of fifth element : " + li.previousIndex());
} }
Copy after login

Output:

Java List Iterator

8. remove()

The last element returned on calling next() or previous() will be removed.

Code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Kavya");
nm.add("Dileep");
nm.add("Sam");
nm.add("Anna");
//get list iterator
li=nm.listIterator();
System.out.println("List is : " + nm);
li.next();
li.next();
li.remove();
System.out.println("After calling remove() method : " + nm);
}
}
Copy after login

Output:

Java List Iterator

9. set(E e)

Replaces the last element returned by next() or previous() with the specified element (optional operation).

Code:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Kavya");
nm.add("Dileep");
nm.add("Sam");
nm.add("Anna");
//get list iterator
li=nm.listIterator();
System.out.println("List is : " + nm);
li.next();
li.next();
li.set("Anjali");
System.out.println("After calling set() method : " + nm);
}
}
Copy after login

Output:

Java List Iterator

Conclusion

ListIterator is a bidirectional iterator in Java that iterates elements in a list one-by-one. CRUD operations are supported in ListIterator, unlike normal iterator. In this article, different aspects of ListIterator is discussed in detail.

The above is the detailed content of Java List Iterator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template