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

advanced placement java chapter 10 introduction to arrays
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Advanced Placement Java Chapter 10 
 Introduction to Arrays

slide-2
SLIDE 2

✴ 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.

Objectives

slide-3
SLIDE 3

Vocabulary

✴ array ✴ element ✴ enhanced for loop ✴ index ✴ initializer list ✴ logical size ✴ parallel arrays ✴ physical size ✴ procedural

decomposition

✴ range-bound error ✴ structure chart ✴ subscript

slide-4
SLIDE 4
  • 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. 10.1 Conceptual Overview

slide-5
SLIDE 5
  • III. Three arrays, each containing five elements:

10.1 Conceptual Overview

slide-6
SLIDE 6
  • 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 (.). 10.2 Simple Array Manipulations

slide-7
SLIDE 7
  • 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. 10.2 Simple Array Manipulations

slide-8
SLIDE 8

I. Traversal: a loop that iterates through an array one element at a time.

  • II. Count the occurrences:

10.3 Looping Through Arrays

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++; }

slide-9
SLIDE 9
  • 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.3 Looping Through Arrays

slide-10
SLIDE 10

I. Example: declaring an array of 500 integers.

  • II. Arrays are objects and must be instantiated

before using. 10.4 Declaring Arrays

int[] abc; // An array of integers int[] xyz; // An array of integers abc = new int[500]; xyz = new int[10]; 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.

slide-11
SLIDE 11
  • 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 


  • f the elements from one array to the 

  • ther.

10.4 Declaring Arrays

slide-12
SLIDE 12
  • VI. Two variables can refer the same array
  • bject.

10.4 Declaring Arrays

slide-13
SLIDE 13
  • 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. 10.4 Declaring Arrays

int[] abc = {1, 2, 3, 4, 5}; // abc references an array // of 5 integers

slide-14
SLIDE 14
  • VII. Arrays can be formed from any collections
  • f 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. 10.4 Declaring Arrays

slide-15
SLIDE 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.
slide-16
SLIDE 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.

slide-17
SLIDE 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.
slide-18
SLIDE 18

10.5 Working With Arrays That Are Not Full

  • X. When traversing the array use the logical

size and NOT the physical size. i. Adding elements to the array

  • ii. Deleting elements from the array
  • iii. Searching the array
  • iv. Printing the array
slide-19
SLIDE 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

slide-20
SLIDE 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.

slide-21
SLIDE 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.

slide-22
SLIDE 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.

slide-23
SLIDE 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.

slide-24
SLIDE 24

10.8 Arrays and Methods

  • II. Passing a reference to an object as a 


parameter.

slide-25
SLIDE 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.

slide-26
SLIDE 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; }

slide-27
SLIDE 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.

slide-28
SLIDE 28

10.10 Graphics and GUIs: Changing the View 


  • f 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.
slide-29
SLIDE 29

10.10 Graphics and GUIs: Changing the View 


  • f Student Test Scores
  • III. GUI for the student test scores program.
slide-30
SLIDE 30

10.10 Graphics and GUIs: Changing the View 


  • f Student Test Scores
  • IV. Description of the buttons:
slide-31
SLIDE 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.

slide-32
SLIDE 32

Design, Testing, and Debugging Hints III. Array variables are null until assigned

  • bjects.

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.

slide-33
SLIDE 33

Summary In this chapter, you learned: I. Arrays are collections of similar items or

  • elements. The items in arrays are
  • rdered by position.
  • II. Arrays are useful when a program needs to

manipulate many similar items, such as a group of students or a number of test scores.

slide-34
SLIDE 34

Summary In this chapter, you learned:

  • III. Arrays are objects. Thus, they must be

instantiated and they can be referred to by more than one variable.

  • IV. An array can be passed to a method as a

parameter and returned as a value.

  • V. Parallel arrays are useful for organizing

information with corresponding elements.

slide-35
SLIDE 35

Summary In this chapter, you learned:

  • VI. Two-dimensional arrays store values in a

row-and-column arrangement similar to a table.

  • VII. The enhanced for loop is a simplified

version of a loop for visiting each element of an array from the first position to the last position.

slide-36
SLIDE 36

Summary In this chapter, you learned:

  • VI. Two-dimensional arrays store values in a

row-and-column arrangement similar to a table.

  • VII. The enhanced for loop is a simplified

version of a loop for visiting each element of an array from the first position to the last position.

slide-37
SLIDE 37

Assignments

Book Questions

Section 1 - 3 questions Section 2 - 2 questions Section 3 - 5 questions Section 4 - 4 questions Section 5 - 2 questions Section 6 - 5 questions Section 7 - 2 questions Section 8 - 5 questions Section 9 - 2 questions Case Study - No questions Section 10 - 2 questions

Review Questions

Short Answer - 6 Questions

Programs

Program 1 Program 2 Program 3 Program 4 Program 5 Program 6 Program 7 - See teacher for Case Study code