Presentation on theme: "Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X."— Presentation transcript:
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X
Chapter Contents Specifications for the ADT List – Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending Machine Java Class Library: The Interface List
Specifications for the ADT List A list provides a way to organize data Fig. 4-1 A to-do list.
Specifications for the ADT List Operations on lists – Add new entry – at end, or anywhere – Remove an item – Remove all items – Replace an entry – Look at any entry – Look for an entry of a specific value – Count how many entries – Check if list is empty, full – Display all the entries
Specifications for the ADT List To specify an ADT list – Describe its data – Specify the operations ADT list must be considered in general – Not necessarily a list of strings View specifications
Example Fig. 4-2 The effect of ADT list operations on an initially empty list
Potential Problem Operations add, remove, replace, getEntry work OK when valid position given remove, replace and getEntry not meaningful on empty lists A list could become full, what happens to add ?
Possible Solutions Assume the invalid situations will not occur Ignore the invalid situations Make reasonable assumptions, act in predictable way Return boolean value indicating success or failure of the operation Throw an exception
Redefining Specifications A first draft of an ADT specifications may ignore potential problems – Simplifies the first draft Concentrate on details after major portions of specifications written – Makes the specifications complete After writing specifications, implementations – Write Java statements to use the ADT – Checks understanding, suitability of the specifications
Interface for ADT ListInterface View source codesource code Note – Interface has no data fields, constructors – Methods must be public – Strategy for add, remove, replace, getEntry is to have them return a value – Use of return of a reference in remove and getEntry
Using the ADT List Fig. 4-3 A list of numbers that identify runners in the order in which they finish a race
Using the ADT List Consider the scoring of a running race We wish to note the order in which the runners finish We add each runner's (unique) number to the end of the list When done we display the whole list View sample programsample program
A List is Like a Vending Machine Fig 4-4 A vending machine.
A List is Like a Vending Machine Observations about vending machines Can perform only tasks shown on interface Must understand the tasks Cannot see inside the machine Can use the machine even though dont know what happens inside If inside of machine replaced with new improved version – Interface remains unchanged – Customer uses machine in same way as before
A List is Like a Vending Machine Observations about clients and List ADT Client can perform only operations from the ADT List Client must adhere to specifications Client cannot access data without an ADT operation Client can use the list – even though unable to access entries directly If implementation is changed, client still uses list in same way as before
Java Class Library: The Interface List The standard package contains a list interface – called List Methods provided
List Implementations That Use Arrays Chapter 5 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X
An Analogy Consider a classroom with 40 desks in fixed position – Desks are wasted if less than 40 students – Not enough desks if more than 40 students An array is like the classroom – Each desk an array location
An Analogy Fig. 5-1 A classroom that contains desks in a fixed position.
An Analogy Suppose we have some students in classroom in order alphabetically We add a new student – We desire to maintain the alphabetic order – We must shift some students We remove a student in the middle of the sequence – Again, we must shift some students
Adding a Student Fig. 5-2 Seating a new student between two existing students: at least one other student must move
The Java Implementation Private data fields for implementation of AList – Implements interface ListInterface of Chapter 4 View full specification source code View full specification
AList add() Methods First add method adds a new item at the end of the list First add method – Assign new value at end – Increment length of list Second add method adds item in mid-list Second add method – Requires a utility method, makeRoom() makeRoom() – This shifts elements ahead
Adding Items in Mid-list Fig. 5-3 Making room to insert Carla as third entry in an array.
Adding Items in Mid-list Fig. 5-4 An array of objects contains references to those objects Note: figures in this text portray arrays as if they actually contained objectsfigures in this text
The remove() Method View method remove() View method Must shift existing entries to avoid gap in the array – note method removeGap() removeGap() – Except when removing last entry Method must also handle error situations – When position specified in the remove is invalid – When remove() is called and the list is empty – Invalid call returns null value
Removing a List Entry Fig. 5-5 Removing Bob by shifting array entries.
Other Methods in AList Note other methods in the class Note implements java.io.Serializable – Tells compiler that instances of AList can be written to a file using object serialization
Expanding an Array An array has a fixed size – If we need a larger list, we are in trouble When array becomes full – Move its contents to a larger array (dynamic expansion) – Copy data from original to new location – Manipulate names so new location keeps name of original array
Expanding an Array Fig. 5-6 The dynamic expansion of an array copies the array's contents to a larger second array.
Expanding an Array Fig. 5-7 (a) an array; (b) the same array with two references; (c) the two arrays, reference to original array now referencing a new, larger array
Expanding an Array Code to accomplish the expansion shown in Fig. 5-7, previous slide
A New Implementation of a List Change the isFull to always return false – We will expand the array when it becomes full – We keep this function so that the original interface does not change The add() methods will double the size of the array when it becomes full Now declare a private method isArrayFull – Called by the add() methods Click to view these methods
Java Class Library Has two classes that use dynamic array expansion – ArrayList – Vector Both classes – Found in java.util – Implement interface List – Defined in terms of a generic type
Using a Vector to Implement the ADT List Java's Vector class provides capabilities of an array – Able to expand dynamically – Hides the details of the process Vector – Has methods for manipulating entries – Enables implementing the ADT List
Using a Vector Fig. 5-8 A client uses the methods given in ListInterface, but the implementation of the list uses Vector methods to perform its operations
Using a Vector View elements of the class definition VectorList VectorList Note – Constructors – add methods – replace – remove – getEntry
Using a Vector The add() methods – The first uses the addElement method from the Vector class – The other uses the insertElementAt method The remove() method – Uses the removeElementAt method
Pros and Cons of Array Use for the ADT List When using an array or vector … – Retrieving an entry is fast – Adding an entry at the end of the list is fast – Adding or removing an entry that is between other entries requires shifting elements in the array – Increasing the size of the array or vector requires copying elements