Arrays
- Arrays are a reference type that collect multiple values of a given type into a single value.
- The places where values can be stored in an array are called the \i{components} of the array. You can think of the components of an array as a bunch of anonymous variables. That is, they are places in memory that can hold a value just like named variables but they don’t have their own names. Instead each component referred to with an array access expression like
xs[0]
or xs[10]
.
- The elements of an array are accessed using square brackets ([]) and an index, e.g.
xs[0]
accesses the 0th element of the array xs
.
- The values in an array can be either primitive or reference values.
- The size of an array is established at the time of its creation and cannot be changed.
- Arrays have a read-only instance variable
length
which gives the number of elements in the array.
- The valid indices for an array are 0 through length - 1, inclusive.
- Using an invalid index (less than 0 or greater than length - 1) will cause an
ArrayIndexOutOfBoundsException
.
- When an array is created using the keyword
new
and a size (e.g. new int[10]
) all of its elements are initialized with the standard zero value for the element type: 0 for int
s, 0.0
for double
s, false
for boolean
s, and null
for all reference types.
- Initializer lists can be used to create arrays containing specific values, e.g.
new int[] { 42, 52, 103 }
. Arrays created this way have their length set to the number of elements provided.
- An array access expression is like a variable in that it can be used to obtain a value or as a place that can be assigned a value with = or any applicable compound assignment operator (e.g.
+=
or ++
on on an array of numbers).
2d arrays
- 2d arrays are stored as arrays of arrays. Therefore, the way 2d arrays are created and indexed is similar to 1d array objects.
- It is equally correct to think of a 2d array as a 1d array whose components happen to be arrays.
- Sometimes we refer to the non-array values stored in a 2d array as the \i{elements} of the array. So in a 2d array of
int
s the components of the array are 1d arrays of int
s, i.e. int[]
s, but the elements are int
s.
- The square brackets
[row][col]
are used to access and modify an element in a 2d array.
- 2d arrays can be initialized with specific values by writing a nested initialization expression like
{ { 1, 2, 3 }, { 4, 5, 6 } }
- Typically, and always on the AP exam, 2d arrays are arranged with the outer array holding 1d array representing the rows of the 2d array. Thus to access the element at row
r
and column c
, we use arr[r][c]
. This form is called “row-major order”.
- It is, however, possible to think of a 2d array as representing an array of columns in which case to get the element at row
r
and column c
you would write arr[c][r]
. This is called “column-major order”. However on the AP exam all 2d arrays will be dealt with in row-major order.
- All algorithms that work with 1d arrays will work with 2d arrays, as long as they apply to arrays whose elements are arrays.