com s 228 generics instructor: ying cai department of computer science iowa state university
DESCRIPTION
COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University [email protected] Office: Atanasoff 201. Motivation Example. This box allows one to store any kind of objects - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/1.jpg)
COM S 228Generics
Instructor: Ying Cai
Department of Computer ScienceIowa State [email protected]: Atanasoff 201
![Page 2: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/2.jpg)
Motivation Example
This box allows one to store any kind of objectsWhat happens if we want a kind of Box that can store only some specific kind of object?
![Page 3: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/3.jpg)
Motivation Example
This ArrayList allows you to store any object, but this flexibility may result in run-time error
![Page 4: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/4.jpg)
Generic TypesA generic type is a generic class or interface that is parameterized over types
![Page 5: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/5.jpg)
Box<Integer> integerBox; // box of integerintegerBox = new Box<Integer>(); // instantiate a box of integerInteger i = new Integer(1);IntegerBox.set(i);
Box<String> stringBox = new Box<String>(); // box of stringstringBox = new Box<String>();
stringBox.set(“here is a string”);stringBox.set(i); // compile-time error
![Page 6: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/6.jpg)
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???
What flexibility is removed?
![Page 7: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/7.jpg)
Naming Rules
![Page 8: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/8.jpg)
Multiple Type Parameters
Pair<String, Integer> p1 = new OrderedPair<String, Integer>("Even", 8);Pair<String, String> p2 = new OrderedPair<String, String>("hello", "world");orOrderedPair<String, Integer> p1 = new OrderedPair<>("Even", 8); OrderedPair<String, String> p2 = new OrderedPair<>("hello", "world");
![Page 9: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/9.jpg)
Raw TypeA raw type is the name of a generic class or interface without any type arguments.
Box<Integer> integerBox = new Box<>(); // a parameterized boxBox 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
![Page 10: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/10.jpg)
Exercises
![Page 11: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/11.jpg)
Exercises
![Page 12: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/12.jpg)
Exercises
![Page 13: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/13.jpg)
Exercises
![Page 14: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/14.jpg)
Exercises
![Page 15: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/15.jpg)
How can we sort strings, e.g., “apple” < “bear”
![Page 16: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/16.jpg)
Using Comparable Interface
![Page 17: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/17.jpg)
![Page 18: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/18.jpg)
Using Comparator Interface
![Page 19: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/19.jpg)
Using Comparator Interface
![Page 20: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/20.jpg)
![Page 21: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/21.jpg)
Sorting Implementations// Solution 1: For each class, implements its own Sortingint[] selectionSort(int[] arr) {}String[] selectionSort(String[]) {}String[] selectionSort(String[], Comparator) {}Vehicle[] selectionSort(Vehicle[], PriceComparator){}Vehicle[] selectionSort(Vehicle[], WeightComparator){}:::
![Page 22: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/22.jpg)
Generic Sorting Methods
public static <T> void selectionSort ( T[] arr, Comparator<T> Comp )
Type Declaration
![Page 23: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/23.jpg)
Implementation
![Page 24: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/24.jpg)
For each value of T, supply its own Comparator
String arr[] = {“abc”, “1234”)selectionSort(arr, new LengthComparator())
![Page 25: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/25.jpg)
Problems
// suppose you have implemented a Vehicle class // and its comparatorpublic class Vehicle { ... };public VehicleComparator implements Comparator<String> { ... };
// you have also implemented a Truck classpublic class Truck extends Vehicle { ... }
// now you want to sort an array of trucksTruck[] arr;myComp = new VehicleComparator();selectionSort(arr, myComp);
![Page 26: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/26.jpg)
Truck[] arr;selectionSort(arr, myComp);
public static <T> void selectionSort ( T[] arr, Comparator<T> Comp )
![Page 27: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/27.jpg)
public static <T> void selectionSort ( T[] arr, Comparator<? super T> Comp )
Wild Cards and Bounds
Rule of Thumb: If T is a type parameter and you write Comparator<T>, you may want to use Comparator<? super T>.
![Page 28: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/28.jpg)
Generic Sorting
![Page 29: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/29.jpg)
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;
}}
![Page 30: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/30.jpg)
public class Vehicle implements Comparable<Vehicle> { ... public int compareTo(Name n) { ... }}
Vehicle v[]; ...
selectionSort(v);
Using Comparable <T> interface
![Page 31: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/31.jpg)
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 TrucksTruck t[]; ...
selectionSort(t); // does this work?
![Page 32: COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University](https://reader036.vdocuments.site/reader036/viewer/2022062314/5681339f550346895d9ab446/html5/thumbnails/32.jpg)
Using Comparable <T> interface