|
@@ -1,6 +1,8 @@
|
1
|
1
|
package Pair;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
|
4
|
+import java.util.Collections;
|
|
5
|
+import java.util.Comparator;
|
4
|
6
|
|
5
|
7
|
/**
|
6
|
8
|
* In here you must make firstLast, which will return a pair of the first element in the array list and the last
|
|
@@ -10,22 +12,22 @@ import java.util.ArrayList;
|
10
|
12
|
* And a minmax method that returns a pair containing the largest and smallest items from the array list
|
11
|
13
|
*/
|
12
|
14
|
public class Arrays {
|
13
|
|
- public static <E> Pair<E> firstLast(ArrayList<E> a) {
|
|
15
|
+ public static <E extends Comparable<E>> Pair<E> firstLast(ArrayList<E> a) {
|
14
|
16
|
E first = a.get(0);
|
15
|
17
|
E second = a.get(a.size() - 1);
|
16
|
18
|
|
17
|
19
|
return new Pair<>(first, second);
|
18
|
20
|
}
|
19
|
21
|
|
20
|
|
- public <E> E max(ArrayList<E> a) {
|
21
|
|
- return null;
|
|
22
|
+ public <E extends Comparable<E>> E max(ArrayList<E> a) {
|
|
23
|
+ return Collections.max(a);
|
22
|
24
|
}
|
23
|
25
|
|
24
|
|
- public <E> E min(ArrayList<E> a) {
|
25
|
|
- return null;
|
|
26
|
+ public <E extends Comparable<E>> E min(ArrayList<E> a) {
|
|
27
|
+ return Collections.min(a);
|
26
|
28
|
}
|
27
|
29
|
|
28
|
|
- public <E> Pair<E> minmax(ArrayList<E> a) {
|
29
|
|
- return null;
|
|
30
|
+ public <E extends Comparable<E>> Pair<E> minmax(ArrayList<E> a) {
|
|
31
|
+ return new Pair<>(min(a), max(a));
|
30
|
32
|
}
|
31
|
33
|
}
|