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

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Why not just use Arrays? Java ArrayLists.
Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Generics and the ArrayList Class
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Information and Computer Sciences University of Hawaii, Manoa
ArrayList, Multidimensional Arrays
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Data structures Abstract data types Java classes for Data structures and ADTs.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks And Queues Chapter 18.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Computer Science 209 Software Development Inheritance and Composition.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Queues Chapter 8 (continued)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Programming in Java Lecture 11: ArrayList
CSE 143 Lecture 27: Advanced List Implementation
Presentation transcript:

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

Data and Methods Visibility in Java Modifier on members in a class Accessed from the same class Accessed from the same package Accessed from a subclass (child class) Accessed from a different package publicYes protectedYes No (default)Yes No privateYesNo

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

Shrinking and Stretching Arrays ● A Deck holds a varying number of Cards, fewer as Cards are dealt.

Shrinking and Stretching Arrays ● A Card has two fields, rank and suit.

Shrinking and Stretching Arrays

● By specifying a size, we can limit the range of available elements. ● The Deck capacity remains unchanged at 52.

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

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.

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.

Implementing Stacks and Queues ● ArrayStack Class – Similar to Deck class in that it contains an array data and an int size.

Implementing Stacks and Queues ● The isEmpty() method is identical to the one from Deck. ● The pop() method is similar to deal().

Implementing Stacks and Queues ● The peek() method is simpler than pop().

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

Implementing Stacks and Queues

● ArrayQueue Class – 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.

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 Note: size is the number of items currently in this queue.

Implementing Stacks and Queues

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. ● A List can access any data item arbitrarily.

The List Interface

Note: There are two overloaded remove() methods.

The List Interface Input 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 true 1 List [ eggs ] [ eggs bread ] [ eggs bread tea ] [ bread tea ] [ bread ]

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

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()

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

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

The List Interface ● remove(2) (to remove C) shifts all subsequent elements one place to the left.

Iterators ● Often a List must be traversed. ● Because access to the data from outside of the class is limited, an Iterator interface can be used to traverse the List.

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

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().

Iterators ● The Iterable interface requires one method. ● When using Iterators, an enhanced for loop can be used.

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.

Iterators

● Keep going until the sum of the players’ scores is 13. ● A player keeps playing until he must “Go Fish”.

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

Iterators

● Both of the give() and meldSets() 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.

Iterators

Iterators (p150)

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.

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.

Java Collections Framework

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.

Chapter 5 Self-Study Homework ● Pages: ● Exercises: 5.3, 5.5, 5.17, 5.20, 5.21, 5.22 ● Note: If necessary, hand in the error-free program and screenshot of the execution results for each exercise.