In this method, you can check for the existence of more elements in a collection when traversing in backward direction. This method is used with ListIterator
hasPrevious( ) method of ListIterator Interface in java.
In the following example we will show you how can hasPrevious( ) method be implemented in ListIterator interface.
Syntax
boolean hasPrevious( )
This method gives 'true' if listIterator keeps more elements when traversing in backward direction otherwise, returns 'false'.
In this method, you can check for the existence of more elements in a collection when traversing in backward direction. This method is used with ListIterator object. It is useful for checking the availability of more elements at the previous position in a collection.
Parameter description
This method does not take any argument.
Example of hasPrevious( ) method
In this example we will show you how does hasPrevious( ) method work in a collection in backward direction. This example will help you to understand how can you check the availability of more elements in a collection. Through this example we will show you how listIterator( ) method is used and how the hasPrevious( ) method is used for checking the availability of more elements with ListIterator object in a collection in backward direction.
Example:-
package Devmanuals.com; import java.util.List; import java.util.LinkedList; import java.util.ListIterator; public class ListIteratorHasPrevious { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("1"); llist.add("2"); llist.add("3"); llist.add("4"); llist.add("5"); ListIterator litr = llist.listIterator(); while (litr.hasNext()) litr.next(); boolean bol = litr.hasPrevious(); System.out.println("List has elements : " + bol); System.out.println("Elements of list = " + llist); System.out.println("Size of list = " + llist.size()); System.out.println("Elements are in previous order "); while (litr.hasPrevious()) System.out.println(litr.previous()); } }
Output :
List has elements : true Elements of list = [1, 2, 3, 4, 5] Size of list = 5 Elements are in previous order 5 4 3 2 1 |
[ 0 ] Comments