Array Basics
An array holds a group of values, and each value sits at a numbered position.
Why One Variable Per Item Breaks Down
Many programs need to keep track of a group of related values, such as a set of scores, a list of days, or a row of results. A common first instinct is to give each item its own variable. Suppose a program must process the names of one thousand people. The natural first thought is that it needs one thousand variables to hold the names and one thousand print statements to display them. That belief feels reasonable until you see how unworkable it becomes.
What do you think happens?
Before reading on, estimate how many variables and how many print lines a one-variable-per-name program would need for one thousand names.
Reveal answer
One thousand variables and one thousand print statements.
Each name needs its own variable, and each name needs its own line to be printed. The number of lines grows in step with the number of items, which is exactly the problem an array is designed to solve.
An array solves this by keeping the whole list under a single name. One array variable holds the entire collection, and you reach any individual item through its position in that collection. The rest of this article follows one question: when a program wants the value at one particular position, how does it get there?
What an Array Is
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.
In Java, two rules shape this structure. First, every item in the array must be of the same type, called the base type. Second, the numbering always begins at zero. Some other programming languages do not impose the same-type rule, so the same-type requirement is a property of the Java setting rather than of arrays in general.
Length, Base Type, and Index
Three terms describe any array. 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. Keeping these three apart is the key to reading array descriptions correctly.
- int[] : the base type is int, so every item must be an int
- scores : the name of the single variable that holds the whole collection
- {72, 85, 90, 64, 98} : the five items; the length is 5
- the indices run from 0 to 4, so the highest index is always the length minus 1
Read the diagram from left to right. The first score sits at index 0 and the last at index 4. The length is 5, and the highest index is 4, which is one less than the length. This follows directly from numbering that begins at zero, and the same pattern holds for an array of any size.
Elements Are Variables
An array of ints can be understood as a list of variables of type int. An array of Strings can be understood as a list of variables of type String. The source is explicit that an array is not, properly speaking, just a list of integers or strings. It is better thought of as a collection of variables, each of which can hold a value of the array's base type.
The individual variables that make up an array are called its elements. An element is a storage position, identified by its index. The value currently stored in that position is separate from the position itself. Positions label the slots, and values are the contents stored in those slots.
int[] scores = {72, 85, 90, 64, 98};
// scores[1] holds 85Index 1 is the slot that holds 85.
int[] scores = {72, 85, 90, 64, 98};
scores[1] = 88;
// scores[1] now holds 88; index 1 is still the same slotIndex 1 is still the same slot, now holding 88. Only the value changed.
When an element is updated, the slot does not move and its position number does not change. Only the value stored in that slot is replaced. This is why an element behaves like an ordinary variable whose value can be changed at any time.
Random Access by Position
Random access means any element of an array can be accessed directly, without going through the elements before it. The word direct carries the main idea: because each element is identified by its position, the program can jump straight to the position it needs.
64- scores[3] names the position 3 in brackets
- the program returns the value stored there in a single step
- the program does not loop through the array to find the value
Changing the number inside the brackets changes which element is returned, and the program reaches any of them in the same single step. Whether the position is the first, the last, or one in the middle makes no difference to how the program reaches it. Position 3 does not require checking positions 0, 1, and 2 first.
What do you think happens?
Given int[] scores = {72, 85, 90, 64, 98}; what does scores[4] return?
Reveal answer
98
Index 4 is the last position in an array of length 5, and it holds 98. The program reaches it directly, without visiting indices 0 through 3.
The Birthday Problem as a Lookup Pattern
The birthday problem asks how many people must be chosen at random until two share a birthday on the same day of the year. The source uses this problem to demonstrate random access to array elements. The problem involves recording a choice for each day of the year and then checking whether a day has been chosen before. Each day corresponds to a position, so the program must be able to look up a specific position at any moment without scanning all of the others.
A random selection of a day becomes a random selection of a position. The program picks a position number at random, goes straight to that slot, and checks or records what is there. Because random access makes each lookup a single direct step, this kind of repeated checking stays straightforward.
Mistakes Beginners Make with Positions
Common mistake Counting the first element as index 1
What it looks likeReading int[] scores = {72, 85, 90, 64, 98}; and expecting scores[1] to return 72.
Why it failsNumbering in Java always begins at zero, so the first item is at index 0 and scores[1] returns 85.
Do this insteadStart counting at 0. The first element is scores[0], and the last element of a length 5 array is scores[4].
Common mistake Confusing the position with the value stored there
What it looks likeBelieving that changing scores[1] to 88 moves the value 85 to a new index.
Why it failsThe index labels a slot, and the value is the content of that slot. Updating an element changes only the content.
Do this insteadTreat index 1 as the fixed slot and 88 as its new value.
Common mistake Assuming the items can be of mixed types
Why it failsIn Java, every item in an array must share the same base type.
Do this insteadChoose one base type, such as int, for the whole array.
Practice: Tracing an Access
Given int[] scores = {72, 85, 90, 64, 98};, state the length, the base type, and the index of the element holding 90. Then trace what the program returns for scores[3].
Hints
- The length is the number of items in the braces.
- The base type is named in the declaration before the brackets.
- Count positions starting from 0, not 1.
Key Takeaways
- An array is a data structure whose items form a numbered sequence, so one array variable can hold a whole collection.
- Random access means any element can be reached directly by its index, without passing through the elements before it.
- The length is the number of items, the base type is the shared type of those items, and the index is an item's position number, which starts at 0 and runs to length minus 1.
- Each element is a variable that holds a value of the base type; the index names the slot, and the value is its current content.