FileDocCategorySizeDatePackage
Collection1.javaAPI DocExample5361Mon Apr 06 18:10:26 BST 1998c08.newcollections

Collection1

public class Collection1 extends Object

Fields Summary
Constructors Summary
Methods Summary
public static java.util.Collectionfill(java.util.Collection c, int start, int size)

    for(int i = start; i < start + size; i++)
      c.add(Integer.toString(i));
    return c;
  
public static java.util.Collectionfill(java.util.Collection c, int size)

    return fill(c, 0, size);
  
public static java.util.Collectionfill(java.util.Collection c)

    return fill(c, 0, 10);
  
public static voidmain(java.lang.String[] args)

    Collection c = newCollection();
    c.add("ten");
    c.add("eleven");
    print(c);
    // Make an array from the List:
    Object[] array = c.toArray(); 
    // Make a String array from the List:
    String[] str = 
      (String[])c.toArray(new String[1]);
    // Find max and min elements; this means
    // different things depending on the way
    // the Comparable interface is implemented:
    System.out.println("Collections.max(c) = " +
      Collections.max(c));
    System.out.println("Collections.min(c) = " +
      Collections.min(c));
    // Add a Collection to another Collection
    c.addAll(newCollection());
    print(c);
    c.remove("3"); // Removes the first one
    print(c);
    c.remove("3"); // Removes the second one
    print(c);
    // Remove all components that are in the
    // argument collection:
    c.removeAll(newCollection());
    print(c);
    c.addAll(newCollection());
    print(c);
    // Is an element in this Collection?
    System.out.println(
      "c.contains(\"4\") = " + c.contains("4"));
    // Is a Collection in this Collection?
    System.out.println(
      "c.containsAll(newCollection()) = " + 
      c.containsAll(newCollection()));
    Collection c2 = newCollection(5, 3);
    // Keep all the elements that are in both
    // c and c2 (an intersection of sets):
    c.retainAll(c2);
    print(c);
    // Throw away all the elements in c that
    // also appear in c2:
    c.removeAll(c2);
    System.out.println("c.isEmpty() = " +
      c.isEmpty());
    c = newCollection();
    print(c);
    c.clear(); // Remove all elements
    System.out.println("after c.clear():");
    print(c);
  
public static java.util.CollectionnewCollection()

    return fill(new ArrayList());
    // ArrayList is used for simplicity, but it's
    // only seen as a generic Collection 
    // everywhere else in the program.
  
public static java.util.CollectionnewCollection(int start, int size)

    return fill(new ArrayList(), start, size);
  
public static voidprint(java.util.Collection c)

    for(Iterator x = c.iterator(); x.hasNext();)
      System.out.print(x.next() + " ");
    System.out.println();