Array Basics and Syntax
An array is a numbered sequence of elements that all share one base type, and the array as a whole has type btype[].
Why a Thousand Names Need an Array
Suppose a program must process the names of one thousand people. The first idea many learners reach for is one variable per name, with one thousand variables declared and one thousand print statements to display them. That approach quickly becomes unworkable. An array is a structure designed for exactly this situation: it keeps the whole list under a single name, and each individual item is reached through its position in that list.
What do you think happens?
Before reading on, estimate how many variables and how many print statements the one-variable-per-name approach would need for one thousand names.
Reveal answer
One thousand variables and one thousand print statements, which is why the approach becomes unworkable at scale.
Each item would need its own variable, and each would need its own line of code to display it. The array replaces this with one variable that holds the entire collection.
Arrays as Numbered Sequences
An array is a data structure whose items form a numbered sequence. One array variable can hold a whole list, and each item can be reached by its position number.
Because every item has a position number, any single item can be referred to directly by that number. Java imposes two rules on this structure. First, every item in the array must be of the same type. Second, the numbering always begins at zero. The same-type rule belongs to the Java setting rather than to arrays in general, since some other programming languages do not require it.
Three terms describe any array, and keeping them apart is the key to reading array descriptions correctly. The length is the number of items the array contains. The base type is the type shared by the individual items. The index is the position number of one particular item.
Indexes Are Offsets, Not Counts
Each element in an array has an index, which is its numerical position in the sequence. If the array is named A, then element number i is written A[i]. Numbering starts at zero, so the first element is at position zero, the second at position one, and so on. These position numbers are offsets into the sequence, with the first element sitting at offset zero. They are not labels that happen to be counted from one.
The diagram below shows a five-element array with its values. The elements are A[0] through A[4]. Read from left to right, the first value is at index 0 and the last value is at index 4. A learner who reads the first position as index 1 has made the most common indexing error.
The valid indexes therefore run from 0 through A.length minus 1. In the five-element array, A.length is 5, so the highest valid index is 4. There is no slot at index 5 because the highest offset is always one less than the count.
Length Is Fixed at Creation
The length of an array A is written A.length. It is a property that defines the size of the array, meaning the number of elements it holds. Because length is the count that fixes the valid index range, it matters for every index you write.
Length is determined when the array is created and cannot be changed afterward. Whatever size you choose at creation is the size the array keeps for its entire life. So the number of slots available to you is decided up front, and every index you write must fall inside the range that this fixed length defines.
Base Type and the btype[] Type
The word type appears in two places when you work with arrays, and separating them clears up much of the confusion. The first is the type of each element. Every element in an array has the same type, called the base type of the array. The second is the type of the array itself, considered as a whole. The array's type is built from the base type rather than being a separate, unrelated choice. This is written btype[], where btype stands for the base type.
Read the type name from left to right. In int[], the part before the brackets, int, tells you what each element holds, and the brackets mark that the whole thing is an array of those elements. The same pattern applies to any base type: an array of Strings has base type String and array type String[].
Worked Example: Reading a Five-Name Array
Identifying length, base type, and index
An array holds five names, with the names stored in positions 0 through 4. Identify the length, the base type, and the index of the name Carla, which sits third from the start.
- 1Find the length
The array holds five items, so A.length is 5.
- 2Find the base type
Each item is a name, so every element has base type String, and the array as a whole has type String[].
- 3Find the index of Carla
Carla is the third item counting from the first, but indexes start at zero. The first item is at index 0, the second at index 1, and Carla at index 2.
- 4Check the valid range
The valid indexes run from 0 through A.length minus 1, which is 0 through 4. Index 2 falls inside that range, so it refers to a real element.
Length 5, base type String, array type String[], and Carla is at index 2.
Elements Behave Like Variables
The source describes an array as a collection of variables rather than simply a list of integers or strings. Each position in an array acts as a variable that can hold a value of the base type. The individual variables that make up an array are called its elements. An array of ints can be understood as a list of variables of type int, and an array of Strings as a list of variables of type String.
It helps to separate two things. An element is a storage position, identified by its index. The value currently stored in that position is separate from the position itself. The value of an element can be changed at any time, just as the value of an ordinary variable can be changed. Changing the value does not change the position, its index, or the array's length.
A[2] holds 30Slot A[2] contains the value 30
A[2] holds 75Slot A[2] contains the value 75; the slot itself is still index 2 and the length is still 5
In the diagram, the slot labeled A[2] stays the same position before and after the assignment. Only the value inside it changes, from 30 to 75. The index remains 2, and the length remains 5.
Predicting Out-of-Range Access
The first two statements read the elements at indexes 0 and 4. The third statement refers to index A.length, which is 5, outside the valid range, and fails with ArrayIndexOutOfBoundsException.- A[0] is the first element, because numbering starts at zero.
- A[4] is the last element, because the highest index is A.length minus 1.
- A[A.length] points one slot past the end of the array and is out of range.
What do you think happens?
Given an array A with length 5, what happens when the statement A[A.length] runs?
Reveal answer
It causes an ArrayIndexOutOfBoundsException.
A.length is 5, and index 5 is outside the valid range 0 through 4. The fifth element is at index 4, not index 5.
Mistakes With Indexes and Length
Common mistake Assuming the first element is at index 1
What it looks likeReading A[1] and expecting the first value of a five-element array
Why it failsNumbering always begins at zero, so the first element is at index 0 and A[1] is the second element.
Do this insteadStart counting from zero: A[0] is the first element, A[1] the second, and so on.
Common mistake Using A.length as the last index
What it looks likeWriting A[A.length] to reach the final slot of a five-element array
Why it failsA.length counts the elements, so it equals 5 here, but the highest valid index is one less, 4. Index 5 does not exist.
Do this insteadUse A[A.length - 1] to reach the last element.
Common mistake Thinking an element and its value are the same thing
What it looks likeBelieving that changing A[2] from 30 to 75 moves or renames the slot
Why it failsAn element is a storage position with a fixed index, and the value inside it can change without changing the position.
Do this insteadTreat the index as the address of the slot and the value as the content, which can be replaced at any time.
Common mistake Expecting the length to grow when needed
What it looks likeWriting to an index beyond the current length and expecting the array to extend
Why it failsLength is fixed when the array is created and cannot be changed afterward, so an out-of-range index causes an
ArrayIndexOutOfBoundsException.Do this insteadChoose the size at creation so it covers every index you need, and keep all indexes within 0 through A.length minus 1.
Practice: Reading an Array
An array named scores holds seven integers. State its length, its base type, the array type written in btype[] form, the valid index range, and the index of the last element. Then say which index would cause an ArrayIndexOutOfBoundsException if used.
Hints
- Length is the number of items, which is 7 here.
- The base type is the type shared by every element, and the array type is that base type followed by brackets.
- Valid indexes run from 0 through length minus 1, so the last valid index is 6.
- Index 7, which equals the length, is one position past the end.
Key Takeaways
- An array is a numbered sequence of elements that all share one base type, and the array as a whole has type btype[].
- Indexes start at zero, so A[i] is element number i and the valid range is 0 through A.length minus 1.
- Length counts the elements and is fixed at creation; an index outside the valid range, such as A[A.length], causes an
ArrayIndexOutOfBoundsException. - Each element is a storage position that behaves like a variable, and the value inside it can change while the position and length stay the same.