-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathArrays.java
More file actions
34 lines (28 loc) · 1.05 KB
/
Arrays.java
File metadata and controls
34 lines (28 loc) · 1.05 KB
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
package Pair;
import java.util.ArrayList;
import java.util.Collections;
/**
* In here you must make firstLast, which will return a pair of the first element in the array list and the last
* element in the arraylist.
* You must also make a min method that returns the smallest item in the array list
* A max method that returns the largest item in the arraylist
* And a minmax method that returns a pair containing the largest and smallest items from the array list
*/
public class Arrays {
public static <E extends Comparable> Pair<E> firstLast(ArrayList<E> a) {
Pair pair = new Pair(a.get(0), a.get(a.size()-1));
return pair;
}
public static <E extends Comparable> E min(ArrayList<E> al) {
Collections.sort(al);
return al.get(0);
}
public static <E extends Comparable> E max(ArrayList<E> al) {
Collections.sort(al);
return al.get(al.size()-1);
}
public static <E extends Comparable> Pair<E> minMax(ArrayList<E> al) {
Collections.sort(al);
return firstLast(al);
}
}