Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind Java How To - Largest Element in an 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.
___[] nums = {12, 7, 25, 3, 18};3Order
Put the learning moves in the order that makes the concept easiest to apply.
Explanation: We start by assuming the first element is the largest.
Scan the array and keep track of the biggest value found so far:
Find the Largest Element in an Array
Find the Largest Element in an Array
Scan the array and keep track of the biggest value found so far:
Example
int[] nums = {12, 7, 25, 3, 18};
int largest = nums[0];
for (int n : nums) {
if (n > largest) {
largest = n;
}
}
System.out.println("Largest element: " + largest);Explanation: We start by assuming the first element is the largest. Then we loop through the array and update the variable whenever we find a bigger value.