-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinMaxGener.java
36 lines (32 loc) · 1.16 KB
/
MinMaxGener.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
// GMH-MinMaxGener/MinMaxGener.java
class GenArr<T extends Comparable<T>> {
private T[] arr;
private T min, max;
public GenArr(T[] arr) {
if (arr == null || arr.length == 0)
throw new IllegalArgumentException();
min = arr[0];
max = arr[0];
for (int i = 1; i < arr.length; ++i) {
// we can use compareTo, as we know
// that T extends Comparable
// (and the compiler has checked it)
if (arr[i].compareTo(min) < 0) min = arr[i];
if (arr[i].compareTo(max) > 0) max = arr[i];
}
}
public T getMin() { return min; }
public T getMax() { return max; }
}
public class MinMaxGener {
public static void main(String[] args) {
GenArr<Integer> mnmxI =
new GenArr<>(new Integer[]{3, -2 , -7, 2});
GenArr<String> mnmxS =
new GenArr<>(new String[]{"A", "Z", "C"});
System.out.println("I - min = " + mnmxI.getMin() +
"\nI - max = " + mnmxI.getMax());
System.out.println("S - min = " + mnmxS.getMin() +
"\nS - max = " + mnmxS.getMax());
}
}