1 / 32

COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

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

Download Presentation

COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201

  2. 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?

  3. Motivation Example This ArrayList allows you to store any object, but this flexibility may result in run-time error

  4. Generic Types A generic type is a generic class or interface that is parameterized over types

  5. 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

  6. 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???

  7. Naming Rules

  8. 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");

  9. 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

  10. Exercises

  11. Exercises

  12. Exercises

  13. Exercises

  14. Exercises

  15. How can we sort strings, e.g., “apple” < “bear”

  16. Using Comparable Interface

  17. Using Comparator Interface

  18. Using Comparator Interface

  19. 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){} :::

  20. Generic Sorting Methods Type Declaration public static <T> void selectionSort ( T[] arr, Comparator<T> Comp )

  21. Implementation

  22. For each value of T, supply its own Comparator String arr[] = {“abc”, “1234”) selectionSort(arr, new LengthComparator())

  23. 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);

  24. public static <T> void selectionSort ( T[] arr, Comparator<T> Comp ) Truck[] arr; selectionSort(arr, myComp);

  25. 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>.

  26. Generic Sorting

  27. 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; } }

  28. Using Comparable <T> interface public class Vehicle implements Comparable<Vehicle> { ... public int compareTo(Name n) { ... } } Vehicle v[]; ... selectionSort(v);

  29. 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?

  30. Using Comparable <T> interface

More Related