This lesson expands on Introduction to Arrays by showing how to use arrays to perform meaningful tasks.
Some of these techniques might help you solve the practice problems in the previous set as well, so it would be worth revisiting your solutions.
For every technique here, we'll look at:
- The goal — What problem we are trying to solve
- The design approach — How arrays help us solve it
- The code — A clearly written example program
Retrieve specific values from inside an array (first, last, any index).
To access an element, use:
arrayName[index]
You must know the correct index:
- First element → index
0 - Last element →
array.length - 1
This ensures your code works for any array size.
int[] marks = {72, 85, 90, 66, 88};
System.out.println("First: " + marks[0]);
System.out.println("Last: " + marks[marks.length - 1]);First: 72
Last: 88
Change one value inside the array without touching the others.
Select the index you want to modify and assign a new value.
This is just like assigning a variable—except you choose which element.
int[] marks = {72, 85, 90, 66, 88};
marks[2] = 95; // Update index 2 with a new markProcess every element in the array.
Use an index-based loop that runs from 0 to array.length - 1.
This ensures the loop scales for any array length.
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]);
}72
85
90
66
88
This section covers common operations over entire arrays.
Add all values in the array.
Use a running total and loop through every index.
int sum = 0;
for (int i = 0; i < marks.length; i++) {
sum += marks[i];
}
System.out.println("Sum = " + sum);Compute mean score from a list of numbers.
Use a double calculation:
average = sum / length
double avg = (double) sum / marks.length;
System.out.println("Average = " + avg);Find the smallest and largest elements.
Start with the first element, then compare each remaining one.
int min = marks[0];
int max = marks[0];
for (int i = 1; i < marks.length; i++) {
if (marks[i] < min) {
min = marks[i];
}
if (marks[i] > max) {
max = marks[i];
}
}Check whether a target value exists in the array.
Move index by index until the value is found.
boolean found = false;
int target = 90;
for (int i = 0; i < marks.length; i++) {
if (marks[i] == target) {
found = true;
}
}int pos = -1; // means “not found”
for (int i = 0; i < marks.length; i++) {
if (marks[i] == target) {
pos = i;
}
}Count how many elements satisfy a condition.
Use an index loop and increment a counter whenever the condition is true.
int count = 0;
for (int i = 0; i < marks.length; i++) {
if (marks[i] >= 80) {
count++;
}
}Print array values backwards without changing the array.
Start the loop at length - 1, stop at 0, and move downward.
for (int i = marks.length - 1; i >= 0; i--) {
System.out.println(marks[i]);
}88
66
90
85
72
Complete the following practice problems. Starter code template files can be found in the src/ folder of this repo.
Annotated solutions to these problems can be found here.
Given:
int[] nums = {5, 12, 3, 9, 1};Print each value on its own line using a loop.
5
12
3
9
1
Given an array of 6 integers, replace every value less than 10 with 10.
Print the updated array on one line.
Original array: [4, 11, 2, 19, 10, 7]
Updated: 10 11 10 19 10 10
Ask the user for 5 temperatures, store them in an array, and count how many are above 20.
Enter 5 temperatures:
18
22
25
17
21
There are 3 temperatures above 20.
Create an array of 7 quiz scores.
Compute and print the sum and average.
Scores: [7, 9, 10, 8, 6, 9, 7]
Sum = 56
Average = 8.0
Given:
int[] ages = {18, 22, 17, 30, 25, 20};Print the youngest age.
Youngest age: 17
Ask the user for a number between 1 and 100.
Given:
int[] data = {12, 44, 29, 67, 44, 18};Print “Found” or “Not found”.
Enter a number: 44
Found
Given:
double[] values = {3.5, 1.2, 6.8, 1.2, 3.5};Check if the first and last values are the same.
First and last match: true
Given:
int[] points = {14, 9, 27, 31, 18};Ask the user for a target number and print the index where it appears, or -1 if not found.
Enter a target: 27
Found at index: 2
Ask the user for 5 numbers.
Print them in reverse order.
Enter 5 numbers:
3
9
1
4
7
7 4 1 9 3
Ask the user for 8 test scores.
Compute the average.
Print all scores above the average.
Scores:
10
7
8
9
6
12
11
5
Average = 8.5
Above average: 10 9 12 11