In this method, you can find the position of an element into a list in forward direction.
nextIndex( ) method of ListIterator Interface in java.
In the following example we will show you how can nextIndex( ) method is implemented in ListIterator interface.
Syntax
public int nextIndex( )
This method gives the position of an element that would be returned by a later call to next( ).
In this method, you can find the position of an element into a list in forward direction. It gives the position value of an element which is situated just after the traversed element and would be gave back by a later call to next( ) in a list. This method also returns the size of a list if cursor (iterator) is placed at end of list.
Parameter description
This method takes no argument.
Example of nextIndex( ) method
In this example we will show you how does nextIndex( ) method work in a collection using ListIterator interface. This example will help you to understand how can you find the index position of an element in forward direction into a collection. Through this example we will show you how nextIndex( ) method uses ListIterator object and how it displays index value of an element from list and size of list when iterator is placed at end of list.
Example:-
package Devmanuals.com; import java.util.List; import java.util.LinkedList; import java.util.ListIterator; public class ListIteratorNextIndex { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("11"); llist.add("12"); llist.add("13"); llist.add("14"); llist.add("15"); System.out.println("Elements of list = " + llist); ListIterator litr = llist.listIterator(); while (litr.hasNext() != false) { System.out.println("Element " + litr.next() + " is at position "+ litr.nextIndex()); } System.out.println("Size of list = " + litr.nextIndex()); } }
Output :
Elements of list = [11, 12, 13, 14, 15]
|
[ 0 ] Comments