1.3. Arrays¶
So far in this class, we have used List
s when we need to store many
pieces of data in a single object. Lists provide many advantages for this,
but they are not the only way to group many values into one object.
In this module, we will study another data structure called an array.
Arrays are much more primitive than lists, and are one of the principal building blocks used to build more sophisticated data structures in many programming languages. Although it is often easier to use lists, understanding how arrays work constitutes basic programming knowledge that all computer scientists are expected to know.
An array is a named collection of contiguous storage locations–storage locations that are next to each other–that contain data items of the same type. However, when working with an array, the size will be fixed from the start. You can think of an array as being similar to a series of identical variables all declared in a row, where each individual variable is identified with a number or position in the group (its index).
You may have also noticed that the class ArrayList
uses “array” in its
name. That is because the ArrayList
class uses an array internally to
store the data in the list. However, there are several differences, as
we’ll see below. You’ll often see arrays depicted graphically using
diagrams like this:

1.3.1. Accessing Items in Arrays¶
However, arrays access the values a little bit differently, making use of the square brackets again.
When referencing elements in an array, we refer to the position of that
particular element within the array. For example, if the array is
named values
, then the elements are named values[0]
, values[1]
,
values[2]
, … values[n - 1]
, where n gives the number of elements
in the array. This naming also reflects the fact that
the array’s data are contained in storage locations that are next to each
other.
Note that this is the same concept as referring to positions in
a List
or character positions within a String
. The item in the
first “slot” of our array is at index 0 and the last item in the array is
going to be one less than the size of the array. For example,
the first item in an array of size 5 is at index 0, and the last is at
index 4. Trying to access the index 5 would cause
an ArrayIndexOutOfBoundsException
at runtime.
The syntax for referring to elements of an array uses square brackets to provide the desired position (index):
arrayname[subscript]
Here, arrayname is the name of the array (any valid identifier will do) and subscript is the position of the element within the array.
A subscript is an integer quantity contained in square brackets that is
used to identify an array element by its position or index value. A subscript
must be either an integer
value or an integer expression. For example, all the of the following lines
of code are valid ways to access a value in an array values
values[4]
values[x]
values[x + y]
These examples show that when an expression, such as x + y
, is used as a
subscript, it is evaluated to a specific integer value before the reference
is made.
It is a syntax error to use a non-integer type as an array subscript. Each of the following expressions would be invalid:
// will not work!
arr[5.0]
arr["5"]
1.3.2. Arrays Compared to Lists (or ArrayList)¶
1.3.3. Syntax Practice 10a¶
1.3.4. Iterating Over Arrays¶
Lets say we wanted to iterate over all values in an array of integers to print them all out.
We could do this with a counter-controlled loop or a for-each loop.
1.3.4.1. Counter-Controlled Loops Over Arrays¶
To create a numeric for loop over an array, we need to know how many slots
the array has. Fortunately, every array knows its own length, and we can
access it using its length
field.
int[] values = new int[ ... ];
for (int i = 0; i < values.length; i++)
{
values[i] = 2 * i;
}
Notice here that we use values.length
to access the array’s length. Unlike
most other objects where you would use a method, arrays have no useful methods
and provide their length in a special read-only field called length
.
Don’t get this confused with the length()
method on strings or the
size()
method on lists–it is just a field, so you never include
parentheses after the field name.
Remember that since the size of an array can’t be changed once it has been
created, you cannot assign a value to the length
field of the array–it
is read-only.
To summarize this numeric for loop:
The first subscript we want to use is at postion 0, so we create a new variable called
i
and initialize it to 0.Our last subscript position is one less than the length of the array. This means we should stop when our counter is no longer less than the length of the array. Thus, our loop condition is
i < values.length
.We want to go through every index in the array so we write
i++
for the update step to incrementi
by one each time the loop repeats.
When working with both List
s and arrays, it’s very easy to mix up when
to use the size()
method and when to use length
. Equally tricky is
that when accessing the length of a String variable, we’d use
the method length()
.
String[] words = new String[3];
int x = words.length; // x is set to 3
String str = "Hello";
int y = str.length(); // y is set to 5
List<String> moreWords = new ArrayList<>();
int z = moreWords.size(); // z is set to 0
Be sure to keep careful track of what type of data you’re working with so you can access its length correctly.
1.3.4.2. For-Each Loops Over Arrays¶
For-each loops over arrays work exactly the same as with lists or other structures:
String[] coffees = {"Espresso", "Mocha", "Decaf", "Americano"};
for (String coffee : coffees)
{
System.out.println(coffee);
}
However, there are two critical differences compared to a numeric for loop:
You do not have access to the current position or subscript value inside your loop, so you cannot use it in any computations inside the loop.
You can only access each value stored in the array, but cannot change the values stored in the array. The loop variable (for example,
coffee
in this loop) is a local variable inside the loop. While you can assign a new value to the variable, that will not affect the array itself, or the contents of the array.
For-each loops have many advantages, since they are short to write and near bullet-proof in terms of making mistakes with management of the index/position or condition, making it nearly impossible to write infinite loops. However, these advantages do come with limitations. Fortunately, Arrays naturally support either style of for loop, so use the style that best fits your needs.
1.3.5. Applying Arrays in a Problem¶
This video discusses a “Web-Analyzer” project and discusses how arrays could be used in this type of problem. You don’t need to worry too much about what this project does or how it works, or about completing the exercise given in the video. It is just a more detailed example used to explain arrays and how they might be used in a problem.