Type Parameter Bounds : Generic Parameters « Generics « Java Tutorial






import static java.lang.Math.random;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class MainClass {
  public static void main(String[] args) throws Exception {
    LinkedList<Integer> numbers = new LinkedList<Integer>();
    for (int i = 0; i < 10; i++) {
      numbers.addItem(1 + (int) (100.0 * random()));
    }

    System.out.println("\nnumbers list contains:");
    listAll(numbers);

    String filename = "C:/Numbers.bin";
    ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(filename));
    objOut.writeObject(numbers);
    objOut.close();

    LinkedList<Integer> values = null;

    ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(filename));
    values = (LinkedList<Integer>) (objIn.readObject());
    objIn.close();

    System.out.println("\nvalues list contains:");
    listAll(values);
  }

  static void listAll(LinkedList<Integer> list) {
    Integer number = list.getFirst();
    int count = 0;
    do {
      System.out.printf("%5d", number);
      if (++count % 5 == 0) {
        System.out.println();
      }
    } while ((number = list.getNext()) != null);
  }
}
numbers list contains:
   39   63   34   47   86
   88   21   39   40    1

values list contains:
   39   63   34   47   86
   88   21   39   40    1








12.4.Generic Parameters
12.4.1.Using the ? wildcard
12.4.2.Using Bounded Wildcards in Methods
12.4.3.Multiple Type Parameters
12.4.4.Type Parameter Bounds
12.4.5.Constraints on a Wildcard