Arrays
Fixed-size collections and index-based access
6.1 Learning objectives
By the end of this chapter, you should be able to:
- create and initialize arrays;
- stay within valid index bounds;
- choose between a traditional
forloop andfor-each; - compute sum, average, maximum, and minimum values;
- copy an array without sharing the same reference.
6.2 Fixed size and indices

Figure 6.1. Valid indices of an array with four elements.
An array stores multiple values of the same type and has a fixed size. Its indices start at zero. In a four-element array, the valid indices are 0, 1, 2, and 3.
String[] commands = {"hi", "help", "tip", "exit"};
System.out.println(commands[0]); // hi
System.out.println(commands.length); // 4
The language guarantees indexed access and a fixed length; details about the physical memory layout of objects belong to the JVM implementation and are not required to use arrays properly.
6.3 Initialization and default values
int[] counters = new int[3]; // 0, 0, 0
boolean[] states = new boolean[2]; // false, false
String[] replies = new String[2]; // null, null
Primitive arrays receive default values. Object arrays receive null until each slot is populated.
6.4 Iterating safely
public class Chapter06Arrays {
public static void main(String[] args) {
double[] grades = {7.0, 8.5, 10.0, 6.5};
double sum = 0.0;
double highest = grades[0];
for (double grade : grades) {
sum += grade;
if (grade > highest) {
highest = grade;
}
}
double average = sum / grades.length;
System.out.printf("Average: %.2f%n", average);
System.out.printf("Highest grade: %.2f%n", highest);
}
}
Use for-each to read every value. Use an indexed for loop when you need to modify positions or work with two arrays in parallel:
for (int index = 0; index < commands.length; index++) {
System.out.println(index + " -> " + commands[index]);
}
The right condition is index < commands.length. Using <= would attempt to access a position that does not exist.
6.5 Utilities and copying
The Arrays class provides sorting, searching, and string formatting:
import java.util.Arrays;
int[] values = {9, 3, 7, 1};
Arrays.sort(values);
System.out.println(Arrays.toString(values));
Direct assignment shares the same array. To obtain an independent copy:
int[] copy = Arrays.copyOf(values, values.length);
6.6 Project evolution: version 6
Use an array to store the chatbot’s required commands. Iterate over it to build the help message. Its size is still fixed, making visible the limitation that List will solve next.
The complete example is in examples/src/Chapter06Arrays.java.
6.7 Verifiable practice
Create an array with five study durations in minutes, and print the total, average, maximum, and minimum values.
Test cases:
| Input | Expected result |
|---|---|
{30, 40, 20, 50, 10} | total 150, average 30 |
| a single element | maximum, minimum, and average equal the element |
| empty array | must be rejected before accessing index zero |
Empty case
Before accessing
values[0], confirm thatvalues.length > 0.