advanced placement java chapter 10 introduction to arrays · manipulate arrays with loops,...

37
Advanced Placement Java Chapter 10 Introduction to Arrays

Upload: others

Post on 01-Oct-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

Advanced Placement Java

Chapter 10

Introduction to Arrays

Page 2: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

✴ 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

Page 3: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

Vocabulary

✴ array

✴ element

✴ enhanced for loop

✴ index

✴ initializer list

✴ logical size

✴ parallel arrays

✴ physical size

✴ procedural decomposition

✴ range-bound error

✴ structure chart

✴ subscript

Page 4: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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

Page 5: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

III. Three arrays, each containing five elements:

10.1 Conceptual Overview

Page 6: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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

Page 7: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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

Page 8: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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

Page 9: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

III. Other examples:i. Sum the elementsii. Determine presence of absence of a

numberiii. Determine first location

IV. To work with arrays of any size, use the length instance variable in the loop.

10.3 Looping Through Arrays

Page 10: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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 integersint[] 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.

Page 11: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

10.4 Declaring Arrays

Page 12: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

VI. Two variables can refer the same array object.

10.4 Declaring Arrays

Page 13: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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

Page 14: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

10.4 Declaring Arrays

Page 15: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 16: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 17: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 18: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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 arrayii. Deleting elements from the arrayiii. Searching the arrayiv. Printing the array

Page 19: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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

Page 20: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 21: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 22: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 23: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 24: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

10.8 Arrays and Methods

II. Passing a reference to an object as a parameter.

Page 25: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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 arrayand return it using the return statement.

Page 26: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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

Page 27: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 28: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 29: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

10.10 Graphics and GUIs: Changing the View of Student Test Scores

III. GUI for the student test scores program.

Page 30: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

10.10 Graphics and GUIs: Changing the View of Student Test Scores

IV. Description of the buttons:

Page 31: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 32: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 33: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

Summary

In this chapter, you learned:

I. Arrays are collections of similar items or elements. The items in arrays are ordered 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.

Page 34: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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

Page 35: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 36: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

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.

Page 37: Advanced Placement Java Chapter 10 Introduction to Arrays · Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and

Assignments

Book QuestionsSection 1 - 3 questionsSection 2 - 2 questionsSection 3 - 5 questionsSection 4 - 4 questionsSection 5 - 2 questionsSection 6 - 5 questionsSection 7 - 2 questionsSection 8 - 5 questionsSection 9 - 2 questionsCase Study - No questionsSection 10 - 2 questions

Review QuestionsShort Answer - 6 Questions

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