Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind Java How To Find Min and Max in Array?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
___[] numbers = {45, 12, 98, 33, 27};3Order
Put the learning moves in the order that makes the concept easiest to apply.
Explanation: Start by assuming the first element is both the maximum and minimum.
Go through the array and keep track of the highest and lowest values:
Find the Minimum and Maximum Element in an Array
Find the Minimum and Maximum Element in an Array
Go through the array and keep track of the highest and lowest values:
Example
int[] numbers = {45, 12, 98, 33, 27};
int max = numbers[0];
int min = numbers[0];
for (int n : numbers) {
if (n > max) {
max = n;
}
if (n < min) {
min = n;
}
}
System.out.println("Max: " + max);
System.out.println("Min: " + min);Explanation: Start by assuming the first element is both the maximum and minimum. As you loop through the array, update max if you find a larger number and min if you find a smaller number.