An array stores several values of the same element type in order. int[] minutes
means a variable naming an array of ints. An initializer such as
{10, 25, 40} supplies three elements. Indexes start at zero, so those values
occupy positions 0, 1 and 2. An index describes a position, not the element value.
Use minutes.length to get the number of elements. This array property has no
parentheses; String.length() is a method and does have parentheses. The last
valid index is length minus one, but an empty array has no last element at all.
Check boundaries rather than guessing that length itself is a valid index.
Arrays have a fixed size
You can replace an element at an existing position. Doing so changes the array,
not its length. You cannot create a new position by assigning at index length.
new int[3] creates three elements initialized to zero; it does not create an
empty growable container. The next list lesson introduces a growable alternative.
Print individual elements while learning. Printing an array variable directly does not produce a readable list of its contents; it prints an identity-style representation. We will use traversal to produce meaningful reports rather than relying on that representation.
public class Main {
public static void main(String[] args) {
int[] minutes = {10, 25, 40};
System.out.println(minutes[0]);
System.out.println(minutes.length);
minutes[1] = 30;
System.out.println(minutes[1]);
System.out.println(minutes.length);
}
}Check your prediction
10
3
30
3 Replacing the middle value changes only position 1. The array still has three slots. Draw three labeled boxes and keep indexes distinct from their stored minute amounts.
public class Main {
public static void main(String[] args) {
int[] slots = new int[3];
int[] empty = {};
System.out.println(slots[2]);
System.out.println(slots.length);
System.out.println(empty.length);
}
}Check your prediction
0
3
0 slots has three real zero values; empty has no elements. Both are arrays, but only one permits reading index 2. A zero value is not the same thing as a missing element.
public class Main {
public static void main(String[] args) {
int[] values = {10, 20};
System.out.println(values[values.length]);
}
}Read the diagnostic
The program compiles because the expression has an int index. At runtime, index 2 is outside a two-element array. Read the ArrayIndexOutOfBoundsException and change the access to a valid position, or guard an index before using it.
Your turn
Update a fixed plan
Create durations {15,25,10}. Replace the first value with 20. Print the length (3), the new first value (20) and the last value (10). Derive the last index from the length instead of hard-coding 2.
public class Main {
public static void main(String[] args) {
// Create and update the array, then print three facts.
}
}Hint 1: the idea
The size describes how many values exist. The final element is one position before that size.
Hint 2: a first step
Do the assignment before the prints. This particular brief supplies a nonempty array, so its last element exists.
Show one solution
public class Main {
public static void main(String[] args) {
int[] durations = {15, 25, 10};
durations[0] = 20;
System.out.println(durations.length);
System.out.println(durations[0]);
System.out.println(durations[durations.length - 1]);
}
}3
20
10 Change it and explain
Change the plan to a single element and check that length-1 still selects it. Then try an empty array: explain why the formula becomes -1 and must not be used. A formula that works on several nonempty examples still needs a stated precondition or a guard for empty input.
Connect this back to main’s frame: String[] args declares an array of text arguments. The browser supplies no command-line arguments, so that array is empty. Input-box text is standard input, a different channel taught on Day 6.
Completion is saved in this browser only. It does not measure mastery.