Arrays
Objective #1: Use standard arrays appropriately.
- There are several ways to declare and create a one-dimensional array. An array named numbers is being instantiated in the following statements:
int[] numbers = new int[10];
and
int[] numbers = {11, 12, 33, 54, 5, 66, -7, 88, 19, 10};
and
int[] numbers = new int[] {11, 12, 33, 54, 5, 66, -7, 88, 19, 10};
- An array (aka standard array) is actually a reference. It is meant to be used to store numerical values such as int's and doubles. However, you can store objects in an array as in
String[] names = new String[10];
- Unless initialized, the values in an array of int's are all initialized to zero.
- The value stored in an index position of an array is called an element. The first element of an array is considered to be stored in index position zero. This is also sometimes referred to as subscript position zero.
- To assign or change a value stored in the array, you can use a statement like this
numbers[3] = 99;
which stores the value 99 in index position 3. I sometimes refer to index position 3 as the fourth element in an array.
- To access an element in an array, you can use the square brackets as in
System.out.println(numbers[7]);
to make the value 88 appear.
- The length property of an array can be used as in
System.out.println(myArray.length);
which would printout the length of the array named myArray. Note that length is a public property and not a method.
Therefore, parentheses must not be used. Do not confuse this public properity with the public method that is available in the String class.
Note the difference in the following statements:
System.out.println("The length of this array is " + numbersArray.length);
and
System.out.println("The length of this string is " + myName.length());
and
System.out.println("The size of this array list is " + numbersArraList.size());
- Once an array has been created, its length can't be changed. You can expand an array indirectly though by creating a bigger array and then copying the contents of the original array
into the bigger array. The old array will be garbage-collected.
- Be careful, the statement myArray = yourArray; does not copy the elements of the array yourArray into a separate array named myArray. Instead, you are simply copying the reference yourArray into myArray. However, you can use the System.arraycopy method to copy all or part of one standard array to another standard array. This
method is not covered on the AP exam though. The statement
System.arraycopy(fromArray, fromStartPosition, toArray, toStartPosition, numElementsToCopy);
would copy numElementsToCopy elements from the fromArray array starting at its fromStartPosition into the toArray array starting at its toStartPosition.
- If you attempt to access an array index position that is not within the valid range of an array (e.g. myArray[myArray.length] ), the JVM throws an ArrayIndexOutOfBoundsException. This is a run-time error, not a compile error.
Objective #2: Use two dimensional arrays.
- A two-dimensional array (aka matrix or a table) is actually a one-dimensional array of arrays.
- The following statement declares & instantiates a two-dimensional array
that has 3 rows and 4 columns. The first set of square brackets represents
the size of the one-dimensional array (i.e. the number of rows) and the second
set of square brackets represents the length of each of the rows (i.e. the
number of columns).
int[][] grades = new int[3][4];
An alternate way to declare and instantiate a two-dimensional array is to
use an initializer list with curly braces as in
int[][] grades = {{88, 99, 100, 100}, {70, 85, 99, 91}, {85, 90, 23, 76}};
where each row is represented by a list of values in the inner sets of curly
braces and the outer set of curly braces lists the elements in the main one-dimensional
array.
- An assignment statement for a two-dimensional array is typed like this
grades[0][0] = 99;
where the top-left corner element of the two-dimensional array is being assigned
the value 99. Remember the rows and columns of a two-dimensional array are
numbered started at zero.
- A double-nested for loop is often used to iterate through a two-dimensional
array searching for a particular key value as in
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
if (grades[row][col] == 100)
{
System.out.println("I found
a 100");
}
}
}
- The length property of arrays allows you to access the
number of rows in a two-dimensional array. The statement System.out.println(grades.length)); would display 3 since the "outer" one dimensional array has 3 rows. The statement System.out.println(grades[0].length); would display 4 since the length of
the one-dimensional array that is stored in position 0 of the "outer" array
is 4. In other words, the number of columns in the two-dimensional array
grades is 4. Therefore the code segment above could be rewritten to take
advantage of the length property:
for (int row = 0; row < grades.length; row++)
{
for (int col = 0; col < grades[row].length; col++)
{
if (grades[row][col] == 100)
{
System.out.println("I
found
a 100");
}
}
}
- While Java does allow a two-dimensional array to have rows of varying lengths
(called a ragged array or partially-filled array), the AP exam only covers
rectangular two-dimensional arrays where all rows have the same number of
elements.