-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinMaxMeth.java
More file actions
24 lines (21 loc) · 835 Bytes
/
MinMaxMeth.java
File metadata and controls
24 lines (21 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// GMI-MinMaxMeth/MinMaxMeth.java
class MinMaxMeth {
static <T extends Comparable<T>> T getMax(T[] arr) {
if (arr == null || arr.length == 0)
throw new IllegalArgumentException();
T max = arr[0];
for (int i = 1; i < arr.length; ++i)
if (arr[i].compareTo(max) > 0) max = arr[i];
return max;
}
public static void main(String[] args) {
int mxi = getMax(new Integer[]{3, -2 , -7, 2});
// one may enforce type to be substituted for T;
// usually, as here, not needed, as the correct
// type will be inferred by the compiler anyway
String mxs = MinMaxMeth.<String>getMax(
new String[]{"A", "Z", "C"});
System.out.println("I - max = " + mxi);
System.out.println("S - max = " + mxs);
}
}