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 60 61 62 |
/* Iterate through elements of Java LinkedList using ListIterator example This java example shows how to iterate through elements of Java LinkedList using ListIterator's next, hasNext, hasPrevious and previous methods. */ import java.util.ListIterator; import java.util.LinkedList; public class IterateLinkedListUsingListIteratorExample { public static void main(String[] args) { //create LinkedList object LinkedList lList = new LinkedList(); //add elements to LinkedList lList.add("1"); lList.add("2"); lList.add("3"); lList.add("4"); lList.add("5"); /* * To get an ListIterator object of LinkedList, use * ListIterator listIterator() method. */ ListIterator itr = lList.listIterator(); System.out.println("Iterating through elements of Java LinkedList using ListIterator in forward direction..."); while(itr.hasNext()) { System.out.println(itr.next()); } System.out.println("Iterating through elements of Java LinkedList using ListIterator in reverse direction..."); while(itr.hasPrevious()) System.out.println(itr.previous()); } } /* Output would be Iterating through elements of Java LinkedList using ListIterator in forward direction... 1 2 3 4 5 Iterating through elements of Java LinkedList using ListIterator in reverse direction... 5 4 3 2 1 */ |
thank you