forked from TestLeafPages/JavaPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMinInGivenArray.java
More file actions
54 lines (34 loc) · 1.23 KB
/
MaxMinInGivenArray.java
File metadata and controls
54 lines (34 loc) · 1.23 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
package coding;
import java.util.Arrays;
import java.util.Collections;
import org.testng.annotations.Test;
public class MaxMinInGivenArray extends BaseTestNg {
Integer[] array = {1, 45, 77, 88, 33, 23, 4};
@Test(priority=1)
public void usingCollection() {
Integer max = Collections.max(Arrays.asList(array));
System.out.println("Maximum array of number is "+max);
Integer min = Collections.min(Arrays.asList(array));
System.out.println("Minimum array of number is "+min);
}
@Test(priority=2) // Simple and efficient (when the array size is bigger in count)
public void usingArray() {
Arrays.sort(array);
System.out.println("Minimum number in Array "+array[0]);
System.out.println("Maximum number in Array "+array[array.length-1]);
}
@Test(priority=3)
public void usingForLoop() {
int smallNumber =array[0];
int largestNumber =array[0];
for (int i = 0; i < array.length; i++) {
if (array[i]> largestNumber ) {
largestNumber = array[i];
} else if (array[i]<smallNumber) {
smallNumber = array[i];
}
}
System.out.println("Maximum array of number is "+largestNumber);
System.out.println("Minimum array of number is "+smallNumber);
}
}