-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPairTest1.java
33 lines (31 loc) · 963 Bytes
/
PairTest1.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
package pair1;
/**
* @author Cay Horstmann
* @version 1.01 2012-01-26
*/
public class PairTest1 {
public static void main(String[] args) {
String[] words = {"Mary", "had", "a", "little", "lamb"};
Pair<String> mm = ArrayAlg.minmax(words);
System.out.println("min = " + mm.getFirst());
System.out.println("max = " + mm.getSecond());
}
}
class ArrayAlg {
/**
* Gets the minimum and maximum of an array of strings.
*
* @param a an array of strings
* @return a pair with the min and max value, or null if 'a' is null or empty
*/
public static Pair<String> minmax(String[] a) {
if (a == null || a.length == 0) return null;
String min = a[0];
String max = a[0];
for (int i = 1; i < a.length; i++) {
if (min.compareTo(a[i]) > 0) min = a[i];
if (max.compareTo(a[i]) < 0) max = a[i];
}
return new Pair<>(min, max);
}
}