-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathArrays.java
More file actions
38 lines (31 loc) · 1.31 KB
/
Arrays.java
File metadata and controls
38 lines (31 loc) · 1.31 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
35
36
37
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 {
@SuppressWarnings("unchecked")
public static <E extends Comparable> Pair<E> firstLast(ArrayList<E> arrayList) {
return new Pair<E>(arrayList.get(0), arrayList.get(arrayList.size() - 1));
}
@SuppressWarnings("unchecked")
public static <E extends Comparable> E min(ArrayList<E> arrayList) {
Collections.sort(arrayList);
return arrayList.get(0);
}
@SuppressWarnings("unchecked")
public static <E extends Comparable> E max(ArrayList<E> arrayList) {
Collections.sort(arrayList);
return arrayList.get(arrayList.size() - 1);
}
@SuppressWarnings("unchecked")
public static <E extends Comparable> Pair<E> minMax(ArrayList<E> arrayList) {
Collections.sort(arrayList);
return new Pair<E>(arrayList.get(0), arrayList.get(arrayList.size() - 1));
}
}