forked from kbeathanabhotla/DSAAJ2-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chap01.text.03.GenericsTest.java
77 lines (62 loc) · 1.57 KB
/
Chap01.text.03.GenericsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Chap01.text.03.GenericsTest.java
import java.util.*;
public class Solution<Type> {
/**
* limits of generic types
*
* T<int>
*
* instanceof (casting will generate a warning, even successful
*
* not allowed in static context and static methods
*
* cannot instantiate
*
* cannot instantiate a generic type array
*
*/
public static void main(String[] args) {
//Type t;
}
// so that any instance of Shape can be put into shapes
public double totalArea(Collection<? extends Shape> shapes) {
double area = 0.0;
for (Shape shape : shapes) {
if (shape != null)
area += shape.area;
}
return area;
}
// doesn't work if you want to put a square into shapes
public double totalArea2(Collection<Shape> shapes) {
double area = 0.0;
for (Shape s : shapes) if (s != null) area += s.area;
return area;
}
// public static <T> T findMax(T[] arr) { //doesn't work because T might not have compareTo
public static <T extends Comparable<? super T>> T findMax(T[] arr) {
int maxIndex = 0;
for(int i = 1; i < arr.length; i++) {
if(arr[i].compareTo(arr[maxIndex]) > 0) {
maxIndex = i;
}
}
return arr[maxIndex];
}
}
// ordinary generic class
class Map<K, V> {
K key;
V value;
void setKey(K k) {
key = k;
}
V getValue() {
return value;
}
}
class Shape {
double area;
}
class Square extends Shape {
}