chapter 11 arrays continued fundamentals of java

46
Chapter 11 Arrays Continued Fundamentals of Java

Upload: barnard-barton

Post on 01-Jan-2016

258 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 11 Arrays Continued Fundamentals of Java

Chapter 11Arrays Continued

Fundamentals of Java

Page 2: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 2

Objectives

Use string methods appropriately. Write a method for searching an array. Understand why a sorted array can be

searched more efficiently than an unsorted array.

Write a method to sort an array.

Page 3: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 3

Objectives (cont.)

Write methods to perform insertions and removals at given positions in an array.

Understand the issues involved when working with arrays of objects.

Perform simple operations with Java’s ArrayList class.

Page 4: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 4

Vocabulary

Array list Binary search Bubble sort Immutable object Insertion sort

Page 5: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 5

Vocabulary (cont.)

Linear search Selection sort Substring Wrapper class

Page 6: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 6

Advanced Operations on Strings

Most text-processing applications examine and manipulate the characters in strings.– Separating strings into segments– Searching for/replacing specific characters or

substrings– Inserting text into a string

String objects are immutable.– No mutators in the String class

Page 7: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 7

Advanced Operations on Strings (cont.)

Table 11-1: Some commonly used String methods

Page 8: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 8

Advanced Operations on Strings (cont.)

Table 11-1: Some commonly used String methods (cont.)

Page 9: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 9

Advanced Operations on Strings (cont.)

Table 11-1: Some commonly used String methods (cont.)

Page 10: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 10

Advanced Operations on Strings (cont.)

Example 11.2: Count the words and compute the average word length in a sentence.

Page 11: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 11

Advanced Operations on Strings (cont.)

Example 11.2: Count the words and compute the average word length in a sentence (cont.).

Page 12: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 12

Searching

Linear search: Search a data structure (such as an array) from beginning to end

Searching an array of objects:

Page 13: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 13

Searching (cont.)

Binary search: An efficient search algorithm based on eliminating half of the data from the search at each iteration– Data must be sorted first.– Examine midpoint of data, then decide which

half of the data to continue searching on.Discard other half of data.

Page 14: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 14

Searching (cont.)

Binary search code:

Page 15: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 15

Searching (cont.)

Figure 11-1: Trace of a binary search of an array

Page 16: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 16

Searching (cont.)

To compare objects, best if the class implements the Comparable interface– compareTo method

Table 11-2: Behavior of the method compareTo

Page 17: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 17

Searching (cont.)

Binary search for objects:

Page 18: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 18

Searching (cont.)

Implementing a Comparable class example:

Page 19: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 19

Sorting

Arranging the elements of a collection of data (such as an array) in an ordered fashion

Figure 11-2: Array before and after sorting

Page 20: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 20

Sorting: Selection Sort

Basic idea:

Table 11-3: Trace of data during a selection sort

Page 21: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 21

Sorting: Selection Sort (cont.)

Must be able to find smallest number in an array and swap items in an array

Page 22: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 22

Sorting: Bubble Sort

Pass through array comparing adjacent elements– If out of order, swap.

Table 11-4: Trace of data during one pass of a bubble sort

Page 23: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 23

Sorting: Bubble Sort (cont.)

Pseudocode:

Fewer data exchanges than selection sort– Sort can stop early if array already sorted

Page 24: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 24

Sorting: Insertion Sort

After kth pass of sorting loop (k starting at 1), first k items should be in sorted order.

Table 11-4: Trace of data during an insertion sort

Page 25: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 25

Sorting: Insertion Sort (cont.)

Pseduocode:

Page 26: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 26

Sorting (cont.)

Any of the search algorithms can be altered to support sorting of objects.– Object’s class(es) should implement Comparable

Have compareTo method

Example:

Page 27: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 27

Insertions and Removals

Steps for insertion:– 1. Check for available space.– 2. Check validity of target index.

Between 0 and logical size

– 3. Shift items from logical end of array to target index down by one position.

– 4. Assign new item to cell at target index.– 5. Increment logical size by one.

Page 28: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 28

Insertions and Removals (cont.)

Figure 11-3: Inserting an item into an array

Page 29: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 29

Insertions and Removals (cont.)

Steps for removal:– 1. Check validity of target index.

Between 0 and logical size

– 2. Shift items from target index to logical end of array up by one position.

– 3. Decrement logical size by one.

Page 30: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 30

Insertions and Removals (cont.)

Figure 11-4: Removing an item from an array

Page 31: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 31

When array type is an interface type, abstract class, or superclass of 1+ other classes, array may contain different object types. – Might not all respond to common set of

messages

Working with Arrays of Objects

Page 32: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 32

Working with Arrays of Objects (cont.)

What if you want to perform an operation specific to one of the types in the array?– Can use the instanceOf operator to determine

the specific type of element in the array

Most general arrays have type Object.– Can hold any type of object

Page 33: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 33

The Class java.util.ArrayList

Contains sequence of elements ordered by position– Unlike an array in that:

It uses methods rather than [] to manipulate elements.

It tracks the logical size and physical size.The logical size is 0 when created.

– Size automatically adjusted as needed The positions available for access range from 0 to

the logical size minus 1.

Page 34: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 34

The Class java.util.ArrayList (cont.)

Generic array list: Programmer must specify element type for the list

Raw array list: Can contain objects of any reference type

Declaring/instantiating a generic array list:

Page 35: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 35

The Class java.util.ArrayList (cont.)

Table 11-6: Some commonly used ArrayList methods

Page 36: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 36

The Class java.util.ArrayList (cont.)

ArrayList objects cannot directly store primitive types.– Must use wrapper classes

Classes that contain the value of a primitive typeBoolean, Integer, Double, Character

Page 37: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 37

The Class java.util.ArrayList (cont.)

ArrayList objects automatically “box” and “unbox” primitive values when used with ArrayList methods.

Page 38: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 38

The Class java.util.ArrayList (cont.)

Advantages of ArrayList over arrays:– Includes many methods for tasks such as

insertions, removals, and searches– Tracks own logical size and grows or shrinks

automatically with the number of elements contained in it

Page 39: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 39

Graphics and GUIs: Menus

A drop-down menu system consists of a menu bar, a number of menus, and several selections for each menu.– May have sub-menus– Menu item object for each menu selection

(class JMenuItem)– Menu object for each menu (class JMenu)– Menu bar object in which all of the menu

objects will appear (class JMenuBar)

Page 40: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 40

Graphics and GUIs: Menus (cont.)

Figure 11-6: New user interface for the student test scores program

Listener objects are attached to menus.– When menu items are selected, events are fired

and the listener objects respond.

Page 41: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 41

Graphics and GUIs: Menus (cont.)

Example 11.6: TestScoresView class (with menus)

Page 42: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 42

Graphics and GUIs: Menus (Cont.)

Example 11.6: TestScoresView class (with menus, cont.)

Page 43: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 43

Summary

Linear search: Simple search that works well for small- and medium-sized arrays

Binary search: Clever search that works well for large arrays but assumes that the elements are sorted

Comparisons of objects are accomplished by implementing the Comparable interface, which requires the compareTo method.

Page 44: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 44

Summary (cont.)

Selection sort, bubble sort, and insertion sort are simple sort methods that work well for small- and medium-sized arrays.

Insertions and removals of elements at arbitrary positions are complex operations that require careful design and implementation.

Page 45: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 45

Summary (cont.)

One can insert objects of any class into an array of Object. When retrieved from the array, objects must be cast down to their classes before sending them most messages.

The limitation of a fixed-size array can be overcome by using Java’s ArrayList class.

Page 46: Chapter 11 Arrays Continued Fundamentals of Java

Fundamentals of Java 46

Summary (cont.)

An array list tracks and updates its logical size and provides many useful client methods.

Wrapper class, such as Integer, provides a way of packaging a value of a primitive type, such as int, in an object so that it can be stored in an array of Object or an array list.