-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMinArray.java
More file actions
147 lines (122 loc) · 4.77 KB
/
Copy pathMaxMinArray.java
File metadata and controls
147 lines (122 loc) · 4.77 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package arrays;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.OptionalInt;
public class MaxMinArray {
/*
* Initialize variables max and min with the first element of the array.
* Iterate through each element in the array.
* If the current element is greater than max, update max.
* If the current element is less than min, update min.
* Print the final values of max and min.
*/
public static void main(String[] args) {
int[] numbers = { 7, 2, 9, 1, 5 };
int max = numbers[0];
int min = numbers[0];
for (int num : numbers) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
System.out.println("Maximum: " + max);
System.out.println("Minimum: " + min);
// Java 8 Stream API - Start
// Example array
int[] numbersArray = { 7, 2, 9, 1, 5 };
findAndPrintMinMax(numbersArray);
// Edge cases
int[] emptyArray = {}; // Empty array
findAndPrintMinMax(emptyArray);
int[] allSameNumbers = { 4, 4, 4, 4 }; // All elements are the same
findAndPrintMinMax(allSameNumbers);
int[] negativeNumbers = { -3, -7, -1, -10 }; // All negative numbers
findAndPrintMinMax(negativeNumbers);
// Java 17 code
// Example arrays
int[] numbers1 = { 7, 2, 9, 1, 5 };
int[] emptyArray1 = {};
int[] allSameNumbers1 = { 4, 4, 4, 4 };
int[] negativeNumbers1 = { -3, -7, -1, -10 };
// Process each array
processArray(numbers1, "Example Array");
processArray(emptyArray1, "Empty Array");
processArray(allSameNumbers1, "Array with All Same Numbers");
processArray(negativeNumbers1, "Array with Negative Numbers");
}
private static void findAndPrintMinMax(int[] numbers) {
// Use Java Streams to find min and max
OptionalInt max = Arrays.stream(numbers).max();
OptionalInt min = Arrays.stream(numbers).min();
// Print results
System.out.println("Array: " + Arrays.toString(numbers));
if (numbers.length == 0) {
System.out.println("The array is empty, no min or max values.\n");
} else {
System.out
.println("Maximum: " + max.orElseThrow(() -> new IllegalStateException("Unexpected empty array")));
System.out
.println("Minimum: " + min.orElseThrow(() -> new IllegalStateException("Unexpected empty array")));
System.out.println();
}
}
// Java 8 Stream API - Completed
// Java 17 code - start
private static void processArray(int[] numbers, String arrayName) {
System.out.println(arrayName + ": " + Arrays.toString(numbers));
// Using Streams to find min and max
try {
OptionalInt max = Arrays.stream(numbers).max();
OptionalInt min = Arrays.stream(numbers).min();
if (numbers.length == 0) {
throw new IllegalArgumentException("Array is empty, no minimum or maximum value.");
}
System.out.println("Maximum (Stream): " + max.orElseThrow());
System.out.println("Minimum (Stream): " + min.orElseThrow());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
// Using IntSummaryStatistics (Another approach)
try {
IntSummaryStatistics stats = Arrays.stream(numbers).summaryStatistics();
if (numbers.length == 0) {
throw new IllegalArgumentException("Array is empty, statistics cannot be calculated.");
}
System.out.println("Maximum (Statistics): " + stats.getMax());
System.out.println("Minimum (Statistics): " + stats.getMin());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
System.out.println();
}
// Java 17 code - end
// Example Outputs:
/*
* Example Array: {7, 2, 9, 1, 5}
* Example Array: [7, 2, 9, 1, 5]
* Maximum (Stream): 9
* Minimum (Stream): 1
* Maximum (Statistics): 9
* Minimum (Statistics): 1
*
* Empty Array: []
* Array is empty, no minimum or maximum value.
* Array is empty, statistics cannot be calculated.
*
* Array with All Same Numbers: [4, 4, 4, 4]
* Array with All Same Numbers: [4, 4, 4, 4]
* Maximum (Stream): 4
* Minimum (Stream): 4
* Maximum (Statistics): 4
* Minimum (Statistics): 4
*
* Array with Negative Numbers: [-3, -7, -1, -10]
* Maximum (Stream): -1
* Minimum (Stream): -10
* Maximum (Statistics): -1
* Minimum (Statistics): -10
*/
}