60
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Embed Size (px)

DESCRIPTION

Shrinking and Stretching Arrays ● Array lengths are determined and fixed when allocated. ● Many times implementations require variable lengths of items.

Citation preview

Page 1: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Chapter 5

Array-BasedStructures

© 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Page 2: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Overview

● General-Purpose Array-Based Structures– 5.1 Getting around fixed length arrays– 5.2 Stack and Queue interfaces– 5.3 List interface and the ArrayList class– 5.4 List traversal

Page 3: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

● Array lengths are determined and fixed when allocated.

● Many times implementations require variable lengths of items.

Page 4: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

● A Deck holds a varying number of Cards, fewer as Cards are dealt.

Page 5: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

● A Card has two fields, rank and suit.

Page 6: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

Page 7: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

Page 8: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

Page 9: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

● By specifying a size, we can limit the range of available elements.

● The Deck capacity remains unchanged at 52.

Page 10: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

Page 11: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

● Getting the number of Cards in the Deck and testing for an empty Deck are trivial. i.e. get the size variable.

Page 12: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

● Only method to modify the size is the deal() method which removes and returns one Card from the Deck.

● size is decremented but the array still references the last element.

Page 13: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

● shuffle() should position the Cards randomly through the Deck.

● For each position in the Deck, randomly select another position in the Deck and swap Cards.

Page 14: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Shrinking and Stretching Arrays

● What if a structure needs to grow beyond its original capacity?

● Copy the entire contents of the array into a new, larger array.

Page 15: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

● ArrayStack Class– Similar to Deck class in that it contains an array

data and an int size.

Page 16: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

● The isEmpty() method is identical to the one from Deck.

● The pop() method is similar to deal().

Page 17: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

● The peek() method is simpler than pop().

Page 18: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

● If the push() method is called when the data array is full, the array must be streched.

Page 19: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

Page 20: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

● ArrayQueueClass– Adding something to an ArrayQueue is exactly like

pushing it onto an ArrayStack.● Removing an element must cause the front of

the queue to move along the array.

Page 21: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

● When insertions runs up against the right end of the array, the queue must wrap around, so that the next element added goes at position 0.

or

Page 22: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

Page 23: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

Page 24: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Implementing Stacks and Queues

Page 25: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

The List Interface

● Stacks and queues limit access to the data to one end or the other.

● The List Interface has a more general-purpose use.

● A List is similar to an array, but it does not have a fixed size.

Page 26: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

The List Interface

Page 27: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

The List Interface

Page 28: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

The List InterfaceInput

list.add("eggs");

list.add("bread");

list.add("tea");

list.contains("eggs");

list.contains("rutabagas");

list.get(1);

list.isEmpty();

list.remove(0);

list.remove("llamachow");

list.remove("tea");

list.size();

Return Value

true

false

“bread”

false

false

true

1

List

[ eggs ]

[ eggs bread ]

[ eggs bread tea ]

[ eggs bread tea ]

[ eggs bread tea ]

[ eggs bread tea ]

[ eggs bread tea ]

[ bread tea ]

[ bread tea]

[ bread ]

[ bread ]

Page 29: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

The List Interface

● The ArrayList's structure is similar to the ArrayStack and ArrayQueue.

Page 30: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

The List Interface

● Some methods are trivial:– get(int index)– set(int index, E target)– add(E target)

● Some are identical to the ArrayStack methods:– isEmpty()– isFull()– size()– stretch()

Page 31: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

The List Interface

● contains() method searches the list for an object.

Page 32: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

The List Interface

● remove() removes and returns the item at a given location or removes the first occurrence of a particular item, if any.

Page 33: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

The List Interface

● remove() shifts all subsequent elements one place to the left.

Page 34: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators● Often a List must be traversed.● Because, outside of the class, access to the

data is limited, an Iterator interface can be used to traverse the List.

Page 35: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 36: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators● Iterator general use:

● Warning: Each call to next() advances the Iterator.

Page 37: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators● java.util.NoSuchElementException

– Thrown when the Iterator has reach it's end and next() is called.

– Avoided by preceding all next() calls by a hasNext() test.

● java.util.IllegalStateException– Thrown when next() has never been invoked, or

has not been invoked since the last invocation of remove().

Page 38: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators● The Iterable interface requires one method.

● When using Iterators, an enhanced for loop can be used.

Page 39: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 40: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators● The iterator() method for ArrayList creates an

instance of ArrayIterator.

● The ArrayIterator references the ArrayList.● An int is also used to indicate how far down the

ArrayList we've traveled.● A previous index is useful to the remove()

method.

Page 41: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 42: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 43: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 44: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 45: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 46: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators● Keep going until the sum of the players’ scores

is 13.● A player keeps playing until he must “Go Fish”.

Page 47: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 48: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 49: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators● The computerTurn() method is similar to

playerTurn() method but the rank is chosen randomly.

● Rather than repeat work to covert a number to a rank, a Card of the rank in question is created from the number. The suit doesn't matter (HEARTS is arbitrarily specified).

Page 50: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 51: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 52: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators● Both of the methods in the GoFishHand class

involve Iterators, so we import java.util.Iterator.● give() transfers all Cards of a given rank from

one hand to another and returns a boolean indicating if any were transferred.

● meldSets() removes all sets of 4 same-rank Cards and returns the total number of sets.

Page 53: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 54: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 55: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Iterators

Page 56: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Java Collections Framework

Page 57: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Java Collections Framework

● The java ArrayList is similar to the one implemented in this chapter.

● The Vector class is still present for backward compatibility. New code should use the ArrayList class.

● The framework does not include a Stack interface, but it does include a Stack class that extends the Vector class.

● There is a Queue interface, but no array-based implementation.

Page 58: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Java Collections Framework

● List is implemented by the class AbstractList.● Abstract class instances can only be created by

instantiating non-abstract classes that extend it.● An abstract class is similar to an interface in

that is defines abstract methods.● The difference is that abstract classes may

contain fields and non-abstract methods.

Page 59: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Java Collections Framework

Page 60: Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved

Summary

● Array sizes cannot be changed, but the array-based structures can maintain varying sizes of data.

● Lists such as ArrayList provide basic methods for accessing elements and return Iterators.

● An Iterator allows us to traverse the List, visiting each element.

● Java’s collections framework includes a List interface, an ArrayList class, a Stack class, and a Queue interface.