Array Review CS 139 – November 28, 2007
Terminology – See html page Array Element Subscript Instantiation Initializer list
Array Model int anArray; anArray = new int []; anArray Note: the array elements are initialized to 0 since this is a numeric type and the constructor will initialize numeric fields to 0.
Arrays and loops Before: After: anArray Before: for (int ii = 0; ii < anArray.length; ii++) anArray[ii] = ii; After: 1 2 3 4 5
Checkpoint Do checkpoint 8.1, 8.3, 8.4, 8.6, 8.8
Array length Arrays are objects. As such, arrays “know themselves”. An array knows how many elements it contains. length is a field of the array (or an attribute of the array). NOTE: no ()…length is a method in the String class, an attribute or field in the array class.
length and subscripts We can use length to determine how many elements to process in the array. The largest subscript is the length – 1. So for an array of 20 elements, there are 20 subscripts beginning with 0, so the highest subscript is 19. arrayName.length can be used to control array processing.
final int ARRAY_SIZE = 10; char myArray; myArray = new char[ARRAY_SIZE]; // updating an array – standard for loop for (int ii = 0; ii < myArray.length; ii++) myArray = ‘A’; // reading an array – enhanced for loop for (char letter : myArray) System.out.println(letter);
Enhanced for loop limitations May only be used on a single loop May only be used to “read” the elements of an array. May only process the array from 0 – length – 1; cannot process the array in reverse. Will process all array elements. You do not have access to the subscript.
Checkpoint Do 8.9 (page 454). First draw a model of the two arrays in question. Write the statement. Then write statements to create a new array, numbers3 which will have the same number of elements as numbers1 (do not count). Write a statement that will fill numbers3 with the product of the same position elements in numbers1 and numbers2. Do 8.12.
Reassigning Array References An array reference can be assigned to another array of the same type. // Create an array referenced by the numbers variable. int[] numbers = new int[10]; // Reassign numbers to a new array. numbers = new int[5]; If the first (ten element) array no longer has a reference to it, it will be garbage collected. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.
Reassigning Array References int[] numbers = new int[10]; The numbers variable holds the address of an int array. Address Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.
Reassigning Array References This array gets marked for garbage collection The numbers variable holds the address of an int array. Address numbers = new int[5]; Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.
Copying Arrays You cannot copy an array by merely assigning one reference variable to another. You need to copy the individual elements of one array to another. int[] firstArray = {5, 10, 15, 20, 25 }; int[] secondArray = new int[5]; for (int i = 0; i < firstArray.length; i++) secondArray[i] = firstArray[i]; This code copies each element of firstArray to the corresponding element of secondArray. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.
Passing Arrays as Arguments Arrays are objects. Their references can be passed to methods like any other object reference variable. 5 10 15 20 25 Address showArray(numbers); 30 35 40 public static void showArray(int[] array) { for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); } Example: PassArray.java Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.
Arrays of Objects You must use three steps to create an array of object types. Declare the array Instantiate the array (What are we making when we instantiate the array?) Instantiate the objects that the array will hold This step does not need to happen all at once…think about Boxes
Checkpoint Given a Die class, create an array that will hold 5 Die objects. Roll each of the Die objects in the array. Display each element of the array. Reroll only the 2nd and 3rd elements of the array.