nextIndex( ) method of ListIterator Interface in java.

nextIndex( ) method of ListIterator Interface in java.


Posted in : Core Java Posted on : January 24, 2011 at 6:39 PM Comments : [ 0 ]

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 = [1112131415]
Element 11 is at position 1
Element 12 is at position 2
Element 13 is at position 3
Element 14 is at position 4
Element 15 is at position 5
Size of list = 5

Download Source code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics