1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* Traverse through ArrayList in reverse direction using Java ListIterator Example This Java Example shows how to iterate through an ArrayList object in reverse direction using Java ListIterator's previous and hasPrevious methods . */ import java.util.ListIterator; import java.util.ArrayList; public class TraverseReverseUsingListIteratorExample { public static void main(String[] args) { //create an object of ArrayList ArrayList aList = new ArrayList(); //Add elements to ArrayList object aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"); aList.add("5"); //Get an object of ListIterator using listIterator() method ListIterator listIterator = aList.listIterator(); //Traverse in forward direction System.out.println("Traversing ArrayList in forward direction using ListIterator"); while(listIterator.hasNext()) System.out.println(listIterator.next()); /* Traverse the ArrayList in reverse direction using hasPrevious and previous methods of ListIterator. hasPrevious method returns true if ListIterator has more elements to traverse in reverse direction. Previous method returns previous element in the list. */ System.out.println("Traversing ArrayList in reverse direction using ListIterator"); while(listIterator.hasPrevious()) System.out.println(listIterator.previous()); } } /* Traversing ArrayList in forward direction using ListIterator 1 2 3 4 5 Traversing ArrayList in reverse direction using ListIterator 5 4 3 2 1 */ |
Hi,
If we need to traverse in reverse direction then line no 29 and 30 are overhead.
Instead use following (only reverse direction traversal)
ListIterator listIterator = list.listIterator(list.size());
while(listIterator.hasPrevious())
System.out.println(listIterator.previous());
output is as per expectation !!!