320 likes | 336 Views
COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201. Motivation Example. This box allows one to store any kind of objects
E N D
COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201
Motivation Example This box allows one to store any kind of objects What happens if we want a kind of Box that can store only some specific kind of object?
Motivation Example This ArrayList allows you to store any object, but this flexibility may result in run-time error
Generic Types A generic type is a generic class or interface that is parameterized over types
Box<Integer> integerBox; // box of integer integerBox = new Box<Integer>(); // instantiate a box of integer Integer i = new Integer(1); IntegerBox.set(i); Box<String> stringBox = new Box<String>(); // box of string stringBox = new Box<String>(); stringBox.set(“here is a string”); stringBox.set(i); // compile-time error
What flexibility is removed? ArrayList arr = new ArrayList(): // any object can be stored ArrayList<T>: // only the object of type T can be store ArrayList<String> s = new ArrayList<String>(); s.add(“here”); s.add(“there”); s.add(new Integer(1)); // could be an error??? s.add(1); // could be an error???
Multiple Type Parameters Pair<String, Integer> p1 = new OrderedPair<String, Integer>("Even", 8); Pair<String, String> p2 = new OrderedPair<String, String>("hello", "world"); or OrderedPair<String, Integer> p1 = new OrderedPair<>("Even", 8); OrderedPair<String, String> p2 = new OrderedPair<>("hello", "world");
Raw Type A raw type is the name of a generic class or interface without any type arguments. Box<Integer> integerBox = new Box<>(); // a parameterized box Box rawBox = new Box(); // a raw box Box<String> stringBox = new Box<>(); rawBox = stringBox; // OK integerBox = rawBox; // rawBox is a raw type of Box<T>; // warning: uncheck conversion
Sorting Implementations // Solution 1: For each class, implements its own Sorting int[] selectionSort(int[] arr) {} String[] selectionSort(String[]) {} String[] selectionSort(String[], Comparator) {} Vehicle[] selectionSort(Vehicle[], PriceComparator){} Vehicle[] selectionSort(Vehicle[], WeightComparator){} :::
Generic Sorting Methods Type Declaration public static <T> void selectionSort ( T[] arr, Comparator<T> Comp )
For each value of T, supply its own Comparator String arr[] = {“abc”, “1234”) selectionSort(arr, new LengthComparator())
Problems // suppose you have implemented a Vehicle class // and its comparator public class Vehicle { ... }; public VehicleComparator implements Comparator<String> { ... }; // you have also implemented a Truck class public class Truck extends Vehicle { ... } // now you want to sort an array of trucks Truck[] arr; myComp = new VehicleComparator(); selectionSort(arr, myComp);
public static <T> void selectionSort ( T[] arr, Comparator<T> Comp ) Truck[] arr; selectionSort(arr, myComp);
Wild Cards and Bounds public static <T> void selectionSort ( T[] arr, Comparator<? super T> Comp ) Rule of Thumb: If T is a type parameter and you write Comparator<T>, you may want to use Comparator<? super T>.
Using Comparable <T> interface public static void selectionSort(T[] arr) { for (int i=0; i<arr.length-1; i++) { int minIndex = i; for (int j=i+1; j<arr.length; j++) { if (arr[j].compareTo(arr[minIndex]) < 0) { minIndex = j; } } T temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } }
Using Comparable <T> interface public class Vehicle implements Comparable<Vehicle> { ... public int compareTo(Name n) { ... } } Vehicle v[]; ... selectionSort(v);
Using Comparable () interface public class Vehicle implements Comparable<Vehicle> { ... public int compareTo(Name n) { ... } }; public class Truck extends Vehicle { ... } // now you want to sort an array of Trucks Truck t[]; ... selectionSort(t); // does this work?