Module 7: Arrays and ArrayList
Common Array Patterns: Sum, Max, and Search
Practice the most useful beginner array tasks so grouped data becomes something you can analyze instead of only print.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-16
Java version
Java 25 LTS
Learning goals
- Compute a total from array values
- Find the largest value in an array
- Search for a specific item
Most array work follows a small set of patterns: Add values, compare values, or search for a match.
A running total starts at 0 and grows in a loop: This pattern shows up everywhere from grades to receipts.
Finding a maximum means keeping the best value seen so far: Compare each new value against the current best one.
Searching means checking each element until you find the target or finish the array: It is simple but extremely useful.
Runnable examples
Sum the values in an array
public class Main {
public static void main(String[] args) {
int[] scores = {10, 20, 30};
int total = 0;
for (int score : scores) {
total += score;
}
System.out.println(total);
}
}Expected output
60
Find the maximum value
public class Main {
public static void main(String[] args) {
int[] numbers = {7, 12, 4, 20};
int max = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
}
System.out.println(max);
}
}Expected output
20
Mini exercise
Search an array of names for `"Sara"` and print whether it was found.
Summary
- Array patterns often mean sum, compare, or search.
- A loop plus one tracking variable solves many tasks.
- These patterns prepare you for larger collections later.
Next step
Next, move from fixed-size arrays to ArrayList when the data size should grow or shrink.
Sources used