LinkedList Example
[ Home ] [ Up ] [ Vector/Enumeration Example ] [ ArrayList/Iterator Example ] [ LinkedList Example ] [ Stack Example ] [ Comparable Example ] [ Comparator Example ] [ HashSet Example ] [ TreeSet Example ] [ TreeSet with Comparator ] [ HashMap Example ] [ TreeMap Example ] [ TreeMap with Comparator ]

 

 

Here is the LinkedList example, translated to using generics:

package examples;

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

public class LinkedListExample
{
  public static void main(String[] args)
  {
    List<String> list = new LinkedList<String>();
    list.add("Mary");
    list.add("Frank");
    list.add("Joe");
    list.add("Sylvia");
    list.add("Vanessa");

    for (int i = 0; i < list.size(); i++)
    {
      String name = list.get(i); // Note NO required cast
      System.out.println(i + ": " + name);
    }

    for (ListIterator<String> iter = list.listIterator(); iter.hasNext(); )
    {
      String name = iter.next(); // Note NO required cast
      System.out.println(name);
    }
  }
}

This program produces the output (same as the original):

0: Mary
1: Frank
2: Joe
3: Sylvia
4: Vanessa
Mary
Frank
Joe
Sylvia
Vanessa

 

This page was last modified on 27 November, 2007