An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

Post on 18-Jan-2018

229 views 0 download

description

An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Sort arrays of objects Use two-dimensional and multi-dimensional arrays Use a built-in Arrays class

transcript

An Object-Oriented Approach to An Object-Oriented Approach to

Programming Logic and DesignProgramming Logic and Design

Chapter 8Advanced Array Concepts

An Object-Oriented Approach to Programming Logic and Design 2

ObjectivesObjectives

• Remain within array bounds• Use a for loop to process arrays• Declare an array of objects• Pass arrays to methods• Sort array elements

An Object-Oriented Approach to Programming Logic and Design 3

Objectives (continued)Objectives (continued)

• Sort arrays of objects• Use two-dimensional and multi-dimensional

arrays• Use a built-in Arrays class

An Object-Oriented Approach to Programming Logic and Design 4

Remaining Within Array BoundsRemaining Within Array Bounds

• An array has a finite size• Size measured by number of elements or by

number of bytes in the array• Because an array’s elements are all the same

type, array size in bytes is a multiple of the number of elements

• Out of bounds: when you try to use a subscript that is not within the range of the declared array’s subscripts

An Object-Oriented Approach to Programming Logic and Design 5

Remaining Within Array Bounds Remaining Within Array Bounds (continued)(continued)

An Object-Oriented Approach to Programming Logic and Design 6

Remaining Within Array Bounds Remaining Within Array Bounds (continued)(continued)

An Object-Oriented Approach to Programming Logic and Design 7

Using a Using a forfor Loop to Process Arrays Loop to Process Arrays

• for loop can step through each element of an array

• Set the for loop to start at 0 and end at the highest subscript

• Remember: the highest subscript is one less than the number of elements (array size)

• A named constant can be used for the upper limit of the for loop

An Object-Oriented Approach to Programming Logic and Design 8

Using a Using a forfor Loop to Process Arrays Loop to Process Arrays (continued)(continued)

An Object-Oriented Approach to Programming Logic and Design 9

Using a Using a forfor Loop to Process Arrays Loop to Process Arrays (continued)(continued)

A more efficient way:

An Object-Oriented Approach to Programming Logic and Design 10

Declaring an Array of ObjectsDeclaring an Array of Objects

• An array can hold elements of any type, such as numbers, strings, objects, etc.

• For an array of objects, declare the array with the type of the object (i.e., the class name)

Examples: Employee emp[7]Employee emp[NUM_EMPLOYEES]

An Object-Oriented Approach to Programming Logic and Design 11

Declaring an Array of Objects Declaring an Array of Objects (continued)(continued)

• If the class constructor requires parameters, you must provide the values when you declare the object array

• Example: Employee emp[NUM_EMPLOYEES] = {101, 12.45}, {103, 7.50}, {119, 13.25}, {213, 15.00}, {218, 8.40}, {395, 16.00}, {405, 9.00}

An Object-Oriented Approach to Programming Logic and Design 12

Declaring an Array of Objects Declaring an Array of Objects (continued)(continued)

• When using an object’s method, place the subscript after the array name and before the dot preceding the method name

Example:numeric MAX = NUM_EMPLOYEES – 1for x = 0 to MAX print emp[x].getEmpNum(),” “,

emp[x].getEmpSal() endfor

An Object-Oriented Approach to Programming Logic and Design 13

Passing Arrays to MethodsPassing Arrays to Methods

• An array element can be passed as a parameter just like any simple variable

• An array element is passed “by value” • Only a copy of the value of the array element is

given to the method; the original array element remains unchanged

An Object-Oriented Approach to Programming Logic and Design 14

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

An Object-Oriented Approach to Programming Logic and Design 15

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

An Object-Oriented Approach to Programming Logic and Design 16

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

• You can pass an entire array as a parameter• The array is passed “by reference”: a pointer to

the original array is given to the method• Any changes made to the array in the method are

made to the actual array

An Object-Oriented Approach to Programming Logic and Design 17

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

Example: constant numeric NUM_ELEMENTS = 4 constant numeric MAX = NUM_ELEMENTS - 1 numeric someNums[NUM_ELEMENTS] =

5, 10, 15, 20

methodGetsArray(someNums,MAX)

An Object-Oriented Approach to Programming Logic and Design 18

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

An Object-Oriented Approach to Programming Logic and Design 19

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

An Object-Oriented Approach to Programming Logic and Design 20

Sorting Array ElementsSorting Array Elements

• Sorting: arranging a series of objects in some logical order

• Ascending order: smallest at the beginning, largest at the end

• Descending order: largest at the beginning, smallest at the end

• Sorting can be done by comparing two values, and swapping their positions to achieve the correct order

An Object-Oriented Approach to Programming Logic and Design 21

Sorting Array Elements (continued)Sorting Array Elements (continued)

• Use a temporary variable to hold the current value when swapping values

Example:If valA > valB then temp = valA valA = valB valB = tempendif

An Object-Oriented Approach to Programming Logic and Design 22

Sorting Array Elements (continued)Sorting Array Elements (continued)• Bubble sort: compare pairs of items, swapping if out of

order, until the smallest item “bubbles” up to the top of the list

• Steps:1. Place all values to be sorted into an array2. Compare the first two numbers in the array3. If not in order, swap them4. Compare 3rd value with 2nd value and swap if

necessary5. Continue will all values in same manner

An Object-Oriented Approach to Programming Logic and Design 23

Sorting Array Elements (continued)Sorting Array Elements (continued)

• The list may have to be processed several times to get all items into order.for b = 0 to ARRAY_SIZE - 2 if someNums[b] > someNums[b + 1] then temp = someNums[b] someNums[b] = someNums[b + 1] someNums[b + 1] = temp endifendfor

An Object-Oriented Approach to Programming Logic and Design 24

Sorting Array Elements (continued)Sorting Array Elements (continued)

• At first pass, the largest value “sinks” to the bottom of the list

• Therefore, you do not have to compare the last value on the next pass.

• Each successive pass through the array places the next largest value in the element in the next last position, so one less value needs to be compared

An Object-Oriented Approach to Programming Logic and Design 25

Sorting Array Elements (continued)Sorting Array Elements (continued)

An Object-Oriented Approach to Programming Logic and Design 26

Sorting Arrays of ObjectsSorting Arrays of Objects

• Arrays of objects can be sorted in a similar fashion

• The comparison of objects may be different• Objects are usually compared by comparing a

field value • Example:

Sort an Employee object by calling getEmpSal()

An Object-Oriented Approach to Programming Logic and Design 27

Sorting Arrays of Objects (continued)Sorting Arrays of Objects (continued)

An Object-Oriented Approach to Programming Logic and Design 28

Using Two-Dimensional and Using Two-Dimensional and Multidimensional ArraysMultidimensional Arrays

• A one-dimension array can be envisioned as a column of values.

An Object-Oriented Approach to Programming Logic and Design 29

Using Two-Dimensional and Using Two-Dimensional and Multidimensional Arrays (continued)Multidimensional Arrays (continued)

• A two-dimension array can be envisioned as a table, with rows and columns.

An Object-Oriented Approach to Programming Logic and Design 30

Using Two-Dimensional and Using Two-Dimensional and Multidimensional Arrays (continued)Multidimensional Arrays (continued)

• To declare a two-dimension array, use two sets of brackets for the number of rows and columns

Example: numeric someNumbers[3][4]• Two dimensional array can be initialized in the declaration.Example:numeric someNumbers[3][4] = {8, 9, 10, 11}, {1, 3, 12, 15}, {5, 9, 44, 99}

An Object-Oriented Approach to Programming Logic and Design 31

Using Two-Dimensional and Using Two-Dimensional and Multidimensional Arrays (continued)Multidimensional Arrays (continued)

numeric rents[4][3] = (400, 450, 510}, {500, 560, 630}, {625, 676, 740}, {1000, 1250, 1600}

An Object-Oriented Approach to Programming Logic and Design 32

Using a Built-In Using a Built-In ArraysArrays Class Class

• Many OOP languages provide an Arrays class containing useful methods for manipulating arrays

• Most methods are overloaded to handle different types of data

• If the language you are using does not have a built-in Arrays class, consider building one yourself

An Object-Oriented Approach to Programming Logic and Design 33

Using a Built-In Using a Built-In ArraysArrays Class Class (continued)(continued)

An Object-Oriented Approach to Programming Logic and Design 34

Using a Built-In Using a Built-In ArraysArrays Class Class (continued)(continued)

An Object-Oriented Approach to Programming Logic and Design 35

Using a Built-In Using a Built-In ArraysArrays Class Class (continued)(continued)

An Object-Oriented Approach to Programming Logic and Design 36

SummarySummary

• Subscript values must stay in bounds• for loop is useful for processing every element

in an array• Array can hold elements of any type• When passed as parameters, array elements

are passed by value• When an entire array is passed as a parameter,

it is passed by reference

An Object-Oriented Approach to Programming Logic and Design 37

Summary (continued)Summary (continued)

• Arrays are useful for sorting items into an ascending or descending order

• To sort an array of objects, compare using a field in the object

• Single dimension array is a column of values• Two-dimension array is a table of values• A built-in Arrays class usually provides

common methods for manipulating arrays