LinkedList Iterator and ListIterator Example

opcje binarne xm valencia dating online go here http://racingcitychorus.org/filmo/4974 get link online dating what am i looking for un hombre solo colin firth follow link http://aquanetta.pl/?kostromesp=opcje-binarne-strony&8c8=85 LinkedList Iterator and ListIterator Example

  1. Iterate a LinkedList using Iterator
package linkedlist;

import java.util.Iterator;
import java.util.LinkedList;

public class IteratorExample {

  public static void main(String[] args) {
    // Create a LinkedList
      LinkedList<String> linkedlist = new LinkedList<String>();
   
      // Add elements to LinkedList
      linkedlist.add("Delhi");
      linkedlist.add("Agra");
      linkedlist.add("Mysore");
      linkedlist.add("Chennai");
      linkedlist.add("Pune");
   
      // Obtaining Iterator
      Iterator<String> it = linkedlist.iterator();
   
      // Iterating the list in forward direction
      System.out.println("LinkedList elements:");
      while(it.hasNext()){
         System.out.println(it.next());
      }


  }

}

Output:

LinkedList elements:
Delhi
Agra
Mysore
Chennai
Pune

 

2. Iterate a LinkedList using ListIterator

In this example we will see how to iterate a LinkedList using ListIterator. Using Listterator we can iterate the list in both the directions (forward and backward). Along with traversing, we can also modify the list during iteration, and obtain the iterator’s current position in the list.

package linkedlist;

import java.util.LinkedList;
import java.util.ListIterator;

public class ListIteratorExample {

  public static void main(String[] args) {
    // Create a LinkedList
      LinkedList<String> linkedlist = new LinkedList<String>();
   
      // Add elements to LinkedList
      linkedlist.add("Delhi");
      linkedlist.add("Agra");
      linkedlist.add("Mysore");
      linkedlist.add("Chennai");
      linkedlist.add("Pune");
   
      // Obtaining ListIterator
      ListIterator<String> listIt = linkedlist.listIterator();
   
      // Iterating the list in forward direction
      System.out.println("Forward iteration:");
      while(listIt.hasNext()){
         System.out.println(listIt.next());
      }

      // Iterating the list in backward direction
      System.out.println("\nBackward iteration:");
      while(listIt.hasPrevious()){
         System.out.println(listIt.previous());
      } 

  }

}

Output:

Forward iteration:
Delhi
Agra
Mysore
Chennai
Pune
Backward iteration:
Pune
Chennai
Mysore
Agra
Delhi