advanced placement java chapter 10 introduction to arrays
play

Advanced Placement Java Chapter 10 Introduction to Arrays - PowerPoint PPT Presentation

Advanced Placement Java Chapter 10 Introduction to Arrays Objectives Write programs that handle collections of similar items. Declare array variables and instantiate array objects. Manipulate arrays with loops, including the


  1. 
 Advanced Placement Java Chapter 10 Introduction to Arrays

  2. Objectives ✴ Write programs that handle collections of similar items. ✴ Declare array variables and instantiate array objects. ✴ Manipulate arrays with loops, including the enhanced for loop. ✴ Write methods to manipulate arrays. ✴ Create parallel and two-dimensional arrays.

  3. Vocabulary ✴ array ✴ physical size ✴ element ✴ procedural decomposition ✴ enhanced for loop ✴ range-bound error ✴ index ✴ structure chart ✴ initializer list ✴ subscript ✴ logical size ✴ parallel arrays

  4. 10.1 Conceptual Overview I. The items in an array are called elements . i. All of the elements need to be of the same type. ii. The type can be any primitive or 
 reference type. II. The length of an array is measured by the 
 number of elements. i. The first element is element [0], the second 
 is element [1], etc. ii. An item’s position with an array is its 
 index or subscript.

  5. 10.1 Conceptual Overview III. Three arrays, each containing five elements:

  6. 10.2 Simple Array Manipulations I. The mechanics of manipulating arrays are fairly straightforward. II. First, declare and instantiate the array. <array name>[<index>] i. Index must be between 0 and the length minus 1. ii. The subscript operator ([ ]) has the same precedence as the method selector (.).

  7. 10.2 Simple Array Manipulations III. The JVM checks the values of subscripts before using them. i. Throws an exception if they are out of bounds (less than 0 or greater than array length minus 1). ii. The detection of a range-bound error is similar to the JVM’s behavior when a program attempts to divide by 0. IV. An array’s length is stored in the public instance variable length.

  8. 10.3 Looping Through Arrays I. Traversal : a loop that iterates through an array one element at a time. II. Count the occurrences: int x; // The value searching for in the array int count; // The occurrences of x in the array x = ….; // Set x equal to the value to be searched for count = 0; for (int i = 0; i < 500; i++) { if (abc[i] == x) count++; }

  9. 10.3 Looping Through Arrays III. Other examples: i. Sum the elements ii. Determine presence of absence of a number iii. Determine first location IV. To work with arrays of any size, use the length instance variable in the loop.

  10. 10.4 Declaring Arrays I. Example: declaring an array of 500 integers. int[] abc; // Declares abc to be a variable that can // reference an array of integers abc = new int[500]; // Instantiate an array of 500 integers // for abc to reference. II. Arrays are objects and must be instantiated before using. int[] abc; // An array of integers int[] xyz; // An array of integers abc = new int[500]; xyz = new int[10];

  11. 10.4 Declaring Arrays III. Array variables are null before they are 
 assigned array objects. i. Failure to assign an array object can 
 result in a null pointer exception . IV. Two variables can refer to the same array. V. To have two variables refer to two separate 
 arrays that have the same values, copy all 
 of the elements from one array to the 
 other.

  12. 10.4 Declaring Arrays VI. Two variables can refer the same array object.

  13. 10.4 Declaring Arrays V. Because arrays are objects, Java’s garbage collector sweeps them away when they are no longer referenced. VI. Arrays can be declared, instantiated and initialized in one step. i. The list of numbers between the braces is called an initializer list. int[] abc = {1, 2, 3, 4, 5}; // abc references an array // of 5 integers

  14. 10.4 Declaring Arrays VII. Arrays can be formed from any collections of similar items. i. Booleans, doubles, characters, strings, and students. VIII.Once an array is instantiated, its size cannot be changed, so make sure the array is large enough from the outset.

  15. 10.5 Working With Arrays That Are Not Full I. When an array is instantiated, the computer fills its cells with default values. II. Then the application replaces the values with new ones as needed. III. An application might not use all of the cells available in an array. IV. Physical size : the number of cells in an array. V. Logical size : the number of cells being used.

  16. 10.5 Working With Arrays That Are Not Full VI. Processing Elements in an Array That Is Not Full: i. When the array is not full, one must replace the physical length with its logical size. VII. Adding Elements to an Array: i. Place the element to be added directly after the last available item. ii. Check to see if there is a cell, and change the logical size.

  17. 10.5 Working With Arrays That Are Not Full VIII. Removing Elements from an Array: i. Decrement the logical size, which prevents the application from accessing the garbage elements beyond that point. IX. Arrays and Text Files: i. Text files can be used for output and input.

  18. 10.5 Working With Arrays That Are Not Full X. When traversing the array use the logical size and NOT the physical size. Adding elements to the array i. ii. Deleting elements from the array iii. Searching the array iv. Printing the array

  19. 10.6 Parallel Arrays I. Parallel arrays : using two arrays in which corresponding elements are related. II. Example: i. An array includes strings of people’s names. ii. A second array includes integers of the same people’s ages. String[] name = {“Bill”, “Sue”, “Shawn”, “Mary”, “Ann”}; // name references an array of 5 Strings int[] age = {20, 21, 19, 24, 20}; // age references an array of 5 Strings

  20. 10.7 Using the Enhanced for Loop I. An enhanced for loop visits each element in an array from the first position to the last position. i. On each pass, the element at the current position is assigned a temporary variable. ii. The temporary variable has to be compatible with element type of the array. iii. Allows programmer to skip the use of index variables and other tests.

  21. 10.7 Using the Enhanced for Loop II. A break statement can be used to terminate an enhanced for loop early. III. Enhanced for loops are simpler and less error-prone than for loops with an index.

  22. 10.7 Using the Enhanced for Loop IV. The enhanced for loop cannot be used to: i. Reverse through an array. ii. Assign elements to positions in an array. iii. Track the index position of the current element. iv. Access any element other than the current element on each pass. V. Also, an enhanced for loop shouldn’t be used for an array that’s not filled.

  23. 10.8 Arrays and Methods I. When an object is used as a parameter to a method, what actually gets passed is a reference to the object. i. Not the object itself. ii. The actual and formal parameters refer to the same object. iii. Changes made to the object’s state are in effect after the method terminates.

  24. 10.8 Arrays and Methods II. Passing a reference to an object as a 
 parameter.

  25. 10.8 Arrays and Methods III. Arrays are objects, so the same rules apply. i. When an array is passed as a parameter to 
 a method, the method manipulates the array itself. ii. Changes made to the array in the method are in effect after the method is executed. iii. Passing an array to a method leads to trouble if the method mishandles the 
 array. IV. A method can instantiate a new object or array and return it using the return statement.

  26. 10.8 Arrays and Methods V. Example: copy an array. // Here is the method call. int[] orig = {1, 2, 3 ,4}; int[] copy1 = CopyTwo(orig); //————————————————————————————————————————————————————— int[] CopyTwo (int[] original) // This method will make a copy of the original array into copy. { int[] copy = new int[original.length]; // Makes an integer array the same // length as the original array for (int i = 0; i < original.length; i++) copy[i] == original[i]; return copy; }

  27. 10.9 Arrays of Objects I. Arrays can hold references to objects of any type. II. When an array of objects is instantiated, each cell is null by default until reset to a new object.

  28. 10.10 Graphics and GUIs: Changing the View 
 of Student Test Scores I. Organizing the code between the model and the view splits the code between: i. Managing the interface. ii. Manipulating database. II. A GUI interface to view a database needs buttons to support navigating between records. iii. Also to add or modify records.

  29. 10.10 Graphics and GUIs: Changing the View 
 of Student Test Scores III. GUI for the student test scores program.

  30. 10.10 Graphics and GUIs: Changing the View 
 of Student Test Scores IV. Description of the buttons:

  31. Design, Testing, and Debugging Hints I. To set up an array: i. Declare an array variable. ii. Instantiate an array object and assign it to the array variable. iii. Initialize the cells in the array with data, as appropriate. II. Try to estimate the number of cells needed for an array when creating it.

  32. Design, Testing, and Debugging Hints III. Array variables are null until assigned objects. IV. The index of an array cell ranges from 0 to the length of the array minus 1. V. To access the last cell, use <array>.length-1 . VI. Avoid having more than one array variable refer to the same array object. VII. When an array is not full, track the current number of elements.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend