-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinMaxValue.java
More file actions
61 lines (51 loc) · 1.64 KB
/
MinMaxValue.java
File metadata and controls
61 lines (51 loc) · 1.64 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public final class MinMaxValue {
private MinMaxValue() {
}
static class Limit {
/**
*
*/
private int minvalue;
/**
*
*/
private int maxvalue;
}
static Limit getMinAndMaxValueOfArray(final int[] arr, final int size) {
Limit c = new Limit();
int i;
if (size == 1) {
c.maxvalue = arr[0];
c.minvalue = arr[0];
return c;
}
if (arr[0] > arr[1]) {
c.maxvalue = arr[0];
c.minvalue = arr[1];
} else {
c.maxvalue = arr[1];
c.minvalue = arr[0];
}
for (i = 2; i < size; i++) {
if (arr[i] > c.maxvalue) {
c.maxvalue = arr[i];
} else if (arr[i] < c.minvalue) {
c.minvalue = arr[i];
}
}
return c;
}
/**
* @param args This is the main method array
*/
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings({"VA_FORMAT_STRING_USES_NEWLINE", "VA_FORMAT_STRING_USES_NEWLINE"})
public static void main(final String[] args) {
final int[] arr = {1000, 11, 445, 1, 330, 3000};
final int minmaxsize = 6;
Limit c = getMinAndMaxValueOfArray(arr, minmaxsize);
System.out.printf(" min number is %d", c.minvalue);
System.out.println("");
System.out.printf(" max number is %d", c.maxvalue);
System.out.println("");
}
}