Unit 07 · lesson
An Array Gives Many Values One Indexed Structure
Suppose a program stores five sensor samples.
Repeated variables work, technically:
int sample1 = 12;
int sample2 = 15;
int sample3 = 11;
int sample4 = 18;
int sample5 = 14;
But the program has no single object representing the samples. Processing all five requires repeating operations manually.
An array creates one indexed structure:
int[] samples = {12, 15, 11, 18, 14};
An array separates position from the value stored there
The index identifies a position inside one fixed-length ordered structure.
- ARRAYone fixed-length ordered structureaddress
- INDEXposition from 0 through length minus 1select
- ELEMENTread or replace the value at that positionrepeat
- TRAVERSALvisit every required position or valueproduce
- RESULTgeneralized processing over the structure
The index is not the value. It is the position used to access the value.
Read and write elements
IO.println(samples[0]);
IO.println(samples[3]);
samples[2] = 13;
After the assignment:
index: 0 1 2 3 4
value: 12 15 13 18 14
The array length has not changed. One element changed.
Fixed length is a design property
int[] samples = new int[5];
This allocates an array with exactly five int positions. The default value of each element is 0 until you assign something else.
Arrays are a strong fit when:
- the number of slots is known or intentionally fixed;
- index position has meaning;
- low-overhead ordered storage is useful;
- an API already expects an array.
They are a weaker fit when values need to be added and removed dynamically. Java collections will solve that next.
Traversal turns structure into an algorithm
Indexed traversal:
for (int i = 0; i < samples.length; i++) {
IO.println("sample " + i + " = " + samples[i]);
}
Enhanced for when the position does not matter:
for (int sample : samples) {
IO.println(sample);
}
Both process every element. The choice communicates whether the index itself matters.
Index meaning must be defined
If index 0 means "Monday," index 1 means "Tuesday," and so on, document that rule. Otherwise the position is merely implementation detail.
A dangerous design uses numeric positions as secret codes nobody can explain:
stats[0] means matches
stats[1] means wins
stats[2] means penalties
Later, an object or record may communicate those meanings more clearly.
Build an array-backed report
Use:
int[] runtimes = {42, 39, 44, 38, 41};
Your program should:
- print every runtime;
- print the first runtime;
- print the last runtime using
runtimes.length - 1, not a hard-coded index; - replace one supplied bad reading with a corrected value;
- print the corrected array values.
Before running, draw the index/value table.
Evidence
Submit the table, source, and output. Mark one place where the index is meaningful and one place where only the element value matters.
Then explain why five named scalar variables would make the same processing algorithm harder to generalize. That explanation is the connection to generalized computational solutions, not the presence of square brackets alone.