Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Array Review CS 139 – November 28, 2007 Terminology – See html page Array Element Subscript Instantiation Initializer list Array Model anArray int anArray; anArray = new int []; 0 0 0 0 0 0 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 anArray Before: 0 0 0 0 0 0 for (int ii = 0; ii < anArray.length; ii++) anArray[ii] = ii; After: 0 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. Cha pter 8 Slid e #11 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. Cha pter 8 Slid e #12 Reassigning Array References The numbers variable holds the address of an int array. This array gets marked for garbage collection Address numbers = new int[5]; Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #13 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. Cha pter 8 Slid e #14 Passing Arrays as Arguments Arrays are objects. Their references can be passed to methods like any other object reference variable. showArray(numbers); Address 5 10 15 20 25 30 35 40 Example: PassArray.java public static void showArray(int[] array) { for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); Copyright © 2005, } Starting Out With Java Control Pearson Addison-Wesley. All rights Structures to Objects By Tony Gaddis reserved. Cha pter 8 Slid e #15 Arrays of Objects You must use three steps to create an array of object types. 1. Declare the array 2. Instantiate the array 1. (What are we making when we instantiate the array?) 3. Instantiate the objects that the array will hold 1. 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. Display each element of the array.