Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X.

Similar presentations


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:

1 Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X

2 Chapter Contents Specifications for the ADT List Using 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

3 Specifications for the ADT List
A list provides a way to organize data Fig A to-do list.

4 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

5 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

6 Fig. 4-2 The effect of ADT list operations on an initially empty list
Example Fig The effect of ADT list operations on an initially empty list

7 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?

8 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

9 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

10 Interface for ADT ListInterface
View source 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

11 Using the ADT List Fig. 4-3 A list of numbers that identify runners in the order in which they finish a race

12 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 program

13 A List is Like a Vending Machine
Fig 4-4 A vending machine.

14 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 don’t know what happens inside If inside of machine replaced with new improved version Interface remains unchanged Customer uses machine in same way as before

15 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

16 Java Class Library: The Interface List
The standard package contains a list interface – called List Methods provided

17 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

18 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

19 Fig. 5-1 A classroom that contains desks in a fixed position.
An Analogy Fig. 5-1 A classroom that contains desks in a fixed position.

20 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

21 Adding a Student Fig. 5-2 Seating a new student between two existing students: at least one other student must move

22 The Java Implementation
Private data fields for implementation of AList Implements interface ListInterface of Chapter 4 View full specification source code

23 AList add() Methods First add method adds a new item at the end of the list Assign new value at end Increment length of list Second add method adds item in mid-list Requires a utility method, makeRoom() This shifts elements ahead

24 Adding Items in Mid-list
Fig. 5-3 Making room to insert Carla as third entry in an array.

25 Adding Items in Mid-list
Fig An array of objects contains references to those objects Note: figures in this text portray arrays as if they actually contained objects

26 The remove() Method View method remove()
Must shift existing entries to avoid gap in the array – note method 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

27 Fig. 5-5 Removing Bob by shifting array entries.
Removing a List Entry Fig. 5-5 Removing Bob by shifting array entries.

28 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

29 Expanding an Array An array has a fixed size When array becomes full
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

30 Expanding an Array Fig. 5-6 The dynamic expansion of an array copies the array's contents to a larger second array.

31 Expanding an Array Fig (a) an array; (b) the same array with two references; (c) the two arrays, reference to original array now referencing a new, larger array

32 Expanding an Array Code to accomplish the expansion shown in Fig. 5-7, previous slide

33 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

34 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

35 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

36 Using a Vector Fig A client uses the methods given in ListInterface, but the implementation of the list uses Vector methods to perform its operations

37 Using a Vector View elements of the class definition VectorList <T> Note Constructors add methods replace remove getEntry

38 Using a Vector The add() methods The remove() method
The first uses the addElement method from the Vector class The other uses the insertElementAt method The remove() method Uses the removeElementAt method

39 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


Download ppt "Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X."

Similar presentations


Ads by Google