8000 修改min和max函数。 · mapengfei00099/javaalgorithm@0c2477d · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c2477d

Browse files
author
shengshijun
committed
修改min和max函数。
1 parent 1f4374f commit 0c2477d

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/main/java/ssj/algorithm/math/MathUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static int gcd(int a, int b) {
2323

2424
public static double max(double... vals) {
2525
Preconditions.checkNotNull(vals, "vals should not be null");
26+
Preconditions.checkArgument(vals.length > 0, "vals should not be empty");
2627
if (vals.length == 0) {
2728
return 0;
2829
}
@@ -90,7 +91,7 @@ private static boolean doubleEqual(double one, double two) {
9091
public static <T extends Comparable<? super T>> T min(T... arr) {
9192
Preconditions.checkNotNull(arr);
9293
if (arr.length == 0) {
93-
return null;
94+
throw new ArithmeticException("too few argument");
9495
}
9596

9697
T min_value = arr[0];

src/test/java/ssj/algorithm/Main.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package ssj.algorithm;
22

3+
import ssj.algorithm.math.MathUtil;
4+
5+
import java.lang.reflect.Array;
6+
import java.util.Arrays;
7+
38
/**
49
* Created by shenshijun on 15/2/1.
510
*/
611
public class Main {
712

813
public static void main(String[] args) {
9-
System.out.println(Integer.MAX_VALUE + 2L);
10-
// int i = 2147483648;
14+
Integer[] data = new Integer[]{1, 234};
15+
System.out.println(MathUtil.min(data));
1116
}
1217
}

0 commit comments

Comments
 (0)
0