This method gives previous element (Object) in the list.
In this method, you can find the previous element by iteration, one element at a time from the list. This method is repeatable it may be called repeatedly to iterate through the list in backward direction. The previous( ) method can be combined with next( ) to go backward and forward. This method is used with ListIterator object. It gives the previous value till a collection has more elements. If a collection does not contain more elements, it displays 'NoSuchElementException' exception.
Syntax:public Object previous( )
Example:import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class ListIteratorPrevious {
public static void main(String[] args) {
LinkedList llist = new LinkedList();
llist.add("A");
llist.add("B");
llist.add("C");
llist.add("D");
llist.add("E");
System.out.println("Elements of list = " + llist);
System.out.println("Size of list = " + llist.size());
ListIterator litr = llist.listIterator();
while(litr.hasNext())
litr.next();
System.out.println("Elements in reverse order : ");
System.out.println(litr.previous());
System.out.println(litr.previous());
System.out.println(litr.previous());
System.out.println(litr.previous());
System.out.println(litr.previous());
// For next line the previous( ) method will throws exception
System.out.println("\t-------------THROWS EXCEPTION------------- ");
System.out.println(litr.previous());
}
}
License.
All information of this service is derived from the free sources and is provided solely in the form of quotations.
This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 695 / 159178514. Delta: 0.02886 с