C1.java

import java.util.*;

/**
Some elementary list operations.
*/
class C1
{
    public static void main(String argv[])
    {
        Iterator y;
        Object z;

        LinkedList x = new LinkedList();
        x.add("Tufted titmouse");
        x.add("White-breasted nuthatch");
        x.add("Red-breasted nuthatch");
        x.add("Downy woodpecker");
        x.add("Hairy woodpecker");
        x.add("Red-bellied woodpecker");
        x.add("Junco");
        x.add("House finch");
        x.add("House sparrow");
        x.add("Bluejay");
        x.add("Cardinal");
        x.add("Ruby-throated hummingbird");
        x.add("Sparrow");
        x.add("Carolina wren");
        x.add("Black-capped chickadee");
        x.add("Robin");
        x.add("Cowbird");
        x.add("Catbird");
        x.add("Mockingbird");
        x.add("Grackle");
        x.add("Crow");
        x.add("Red-winged blackbird");
        x.add("Goldfinch");

        System.out.println("The original list:");
        y = x.iterator();
        while (y.hasNext()) {
            z = y.next();
            System.out.println("   " + z);
            }

        // Go through the list, removing items that start with 'C'.
        System.out.println("\nWith all C.. birds removed:");
        y = x.iterator();
        while (y.hasNext()) {
            z = y.next();
            if (((String)z).startsWith("C"))
                y.remove();
            }
        Iterator it = x.iterator();
        while (it.hasNext()) {
            z = it.next();
            System.out.println("   " + z);
            }

        // Construct and print a 5-element sublist.
        System.out.println("\nA five-element sublist:");
        List x2 = x.subList(5,10);
        y = x2.iterator();
        while (y.hasNext()) {
            z = y.next();
            System.out.println("   " + z);
            }
    }
}