Top Banner
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Arrays Chapter 7
22

Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

Apr 12, 2018

Download

Documents

lyhuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Arrays

Chapter 7

Page 2: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Sorting, Searching Arrays:Outline

• Selection Sort• Other Sorting Algorithms• Searching an Array

Page 3: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Selection Sort

• Consider arranging all elements of anarray so they are ascending order

• Algorithm is to step through the array Place smallest element in index 0 Swap elements as needed to accomplish this

• Called an interchange sorting algorithm

Page 4: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Other Sorting Algorithms

• Selection sort is simplest But it is very inefficient for large arrays

• Java Class Library provides for efficientsorting Has a class called Arrays Class has multiple versions of a sort method

Page 5: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Selection Sort

• Figure 7.5a

Page 6: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Selection Sort

• Figure 7.5b

. . .

Page 7: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Selection Sort

• Algorithm for selection sort of an array

Page 8: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Selection Sort

• View implementation of selection sort,listing 7.10 class ArraySorter

• View demo program, listing 7.11class SelectionSortDemo

Sample screenoutput

Sample screenoutput

Page 9: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Multidimensional Arrays: Outline

• Multidimensional-Array Basics• Multidimensional-Array Parameters and

Returned Values• Java's Representation of Multidimensional• Ragged Arrays• Programming Example: Employee Time

Records

Page 10: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Multidimensional-Array Basics

• Consider Figure 7.6, a table of values

Page 11: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Multidimensional-Array Basics

• Figure 7.7 Row and column indices for anarray named table

table[3][2] hasa value of 1262

table[3][2] hasa value of 1262

Page 12: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Multidimensional-Array Basics

• We can access elements of the table witha nested for loop

• Example:

• View sample program, listing 7.12class InterestTable

Page 13: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Multidimensional-Array Basics

Sample screenoutput

Sample screenoutput

Page 14: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Multidimensional-Array Parameters andReturned Values

• Methods can have Parameters that are multidimensional-arrays Return values that are multidimensional-

arrays

• View sample code, listing 7.13class InterestTable2

Page 15: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Java's Representation ofMultidimensional Arrays

• Multidimensional array represented as severalone-dimensional arrays

• Given int [][] table = new int [10][6];

• Array table is actually 1 dimensional of typeint[] It is an array of arrays

• Important when sequencing throughmultidimensional array

Page 16: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Ragged Arrays

• Not necessary for all rows to be of thesame length

• Example:

Page 17: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Programming Example

• Employee Time Records Two-dimensional array stores hours worked

• For each employee

• For each of 5 days of work week

Array is private instance variable of class

• View sample program, listing 7.14class TimeBook

Page 18: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Programming Example

Sample screenoutput

Sample screenoutput

Page 19: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Programming Example

• Figure 7.8 Arrays for the class TimeBook

Page 20: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Summary

• An array is a collection of variables all ofthe same type

• Arrays are objects, created with operatornew

• Elements numbered starting with 0, endingwith 1 less than length

• Indexed variable can be used as aparameter – treated like variable of basetype

Page 21: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Summary

• Entire array can be passed as parameterto a method

• Method return value can be an array• Partially filled array usually stores values

in initial segment, use an int to track howmany are used

• Privacy leak caused by returning arraycorresponding to private instance variable

Page 22: Arrays - Temple Universitytuf80213/courses/temple/cis1068/slides/... · JAVA: An Introduction to Problem Solving & Programming, 6th Ed. ... But it is very inefficient for large arrays

JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter SavitchISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Summary• Selection sort orders an array into

ascending or descending order• Multidimensional arrays are implemented

as an array of arrays• Treat two-dimensional array as a table

with rows and columns