Arrays
Objective #1: Use 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[5];
and
int[] numbers = {11, 12, -33, 54, 5};
and
int[] numbers = new int[] {11, 12, -33, 54, 5};
// the notation in this example is never used on the AP exam
- Usually an array stores integers (int) or decimal numbers (double). But an array can store strings or any other type of object such as Bug's. See the following examples:
int[] scores = new int[5];
double[] averages = new double[5];
String[] names = new String[5];
Bug[] bugCollection = new Bug[5];
- Unless initialized, the values in an array of int's or double's are automatically 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 (aka subscript position zero).
- To assign or change a value stored in the array, you can use an assignment statement like this
numbers[0] = 99;
which stores the value 99 in index position 0. Note that on worksheets and test questions, I refer to index position 0 as the first element of the array, index position 1 as the second element of the array, and so on.
- To display an element in an array, you can use the square brackets as in
System.out.println(numbers[0]);
to make the value 99 appear.
- The length property of an array can be used as in
int[] scores = {88, 77, 62, 98, 70};
System.out.println(scores.length);
which would printout the length of the array named scores which is 5 even though the value 70 is stored in index position 4. 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:
int[] scores = {88, 77, 62, 98, 70};
System.out.println("The length of this array is " + scores.length);
and
String name = "Bob";
System.out.println("The length of this string is " + name.length());
- Once an array has been instantiated, 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 although they never ask you to do something like that on the AP exam.
- If you attempt to access an array index position that is not within the valid range of an array (e.g. scores[scores.length]), the JVM throws an ArrayIndexOutOfBoundsException. This is a run-time error, not a compile error.
- 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. In other words, you are overwriting the memory address of myArray with the one stored yourArray and aliasing the two arrays into one. 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.
Objective #2: Use arrays as instance variables in a class.
Objective #3: Use arrays as parameters and return types.