hasNext( ) method of ListIterator Interface in java.

hasNext( ) method of ListIterator Interface in java.


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

In this method, you can check for further elements in a collection, one element at a time in forward direction when traversing in a collection.

hasNext( ) method of ListIterator Interface in java.

In the following example we will show you how can hasNext( ) method be implemented in ListIterator interface.

Syntax

boolean hasNext( )

This method returns 'true' while traversing the list in forward direction, and a collection keeps more elements otherwise. returns 'false'.

In this method, you can check for further elements in a collection, one element at a time in forward direction when traversing in a collection. This method is used with ListIterator object. It is useful for checking the availability of more elements in the next position in a collection.

Parameter description

This method does not take any argument.

Example of hasNext( ) method

In this example we will show you how does hasNext( ) method work in a collection in forward direction. This example will help you to understand how can you check for existence of more elements in collection. Through this example we will show you how listIterator( ) method is used and how the hasNext( ) method is used for checking the availability of more elements with ListIterator object in a collection.

Example:- 

package Devmanuals.com;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class ListIteratorHasNext {
  public static void main(String[] args) {
    LinkedList llist = new LinkedList();
    llist.add("Bipul");
    llist.add("Kumar");
    llist.add("Choudhary");
    llist.add("Age");
    llist.add("23");
    ListIterator litr = llist.listIterator();
    boolean b = litr.hasNext();
    System.out.println("List has elements : " + b);
    System.out.println("Elements of list = " + llist);
    System.out.println("Size of list = " + llist.size());
}
}

Output : 

List has elements : true

Elements of list = [Bipul, Kumar, Choudhary, Age, 23]

Size of list = 5 

Download Source code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics