Presentation is loading. Please wait.

Presentation is loading. Please wait.

List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.

Similar presentations


Presentation on theme: "List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation."— Presentation transcript:

1 List Implementations That Use Arrays Chapter 5

2 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation Using Dynamic Array Expansion to Implement the ADT List Expanding an Array A New Implementation of a List Using a Vector to Implement the ADT List A Summary of Methods in the Class Vector The Pros and Cons of Using an Array to Implement the ADT List The Class ArrayList The Interface Serializable

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

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

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

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

7 7 The Java Implementation Private data fields for implementation of AList Implements interface ListInterface of Chapter 4 Note the full specification, pgs 103-105 private Object entry[]; // array of list entries private int length; // current number of entries in list private static final int MAX_SIZE = 50; // max length of list

8 8 AList Implementation (1): public class AList implements ListInterface { private T[] entry; // array of list entries private int length; // current number of entries in list private static final int MAX_SIZE = 50; // max length of list public AList() { this(MAX_SIZE); } // end default constructor public AList(int maxSize) { length = 0; entry = (T[]) new Object[maxSize]; } // end constructor public boolean add(Object newEntry) { /* */ } // end add

9 9 AList Implementation (2): public boolean add(int newPosition, T newEntry) { /* */ } // end add public T remove(int givenPosition) { /* */ } // end remove public void clear() { length = 0; // } // end clear public boolean replace(int givenPosition, T newEntry) { /* */ } // end replace public T getEntry(int givenPosition) { /* */ } // end getEntry

10 10 AList Implementation (3): public boolean contains(T anEntry) { /* */ } // end contains public int getLength() { return length; } // end getLength public boolean isEmpty() { return length == 0; } // end isEmpty public boolean isFull() { return length == entry.length; } // end isFull

11 11 AList Implementation (4): public void display() { for (int index = 0; index < length; index++) System.out.println(entry[index]); } // end display /* < This class will define two private methods that will be discussed later. > */ } // end AList

12 12 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 See my Eclipse Code

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

14 14 The remove() Method Must shift existing entries to avoid gap in the array Except when removing last entry Method must also handle error situation When position specified in the remove is invalid When remove() is called and the list is empty Invalid call returns null value

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

16 16 Dynamic Array Expansion 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

17 17 Dynamic Array Expansion Fig. 5-5 The dynamic expansion of an array copies the array's contents to a larger second array.

18 18 Dynamic Array Expansion Fig. 5-6 (a) an array; (b) the same array with two references; (c) the two arrays, reference to original array now referencing a new, larger array

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

20 20 A new add() method (1): public boolean isFull() { return false; } private boolean isArrayFull() { return length == list.length; } public boolean add(T newEntry) { if (isArrayFull()) doubleArray(); // add new entry after last current entry entry[length] = newEntry; length++; return true; } // end add

21 21 A new add() method (2): NOTE: See System.arraycopy() for built-in array copy method. /** Task: Doubles the size of the array of list entries. */ private void doubleArray() { T[] oldList = entry; // save reference to array of // list entries int oldSize = oldList.length; // save old max size of array entry = (T[]) new Object[2*oldSize]; // double size of array // copy entries from old array to new, bigger array for (int index = 0; index < oldSize; index++) entry[index] = oldList[index]; } // end doubleArray

22 22 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 Found in package java.util Has methods for manipulating entries Enables implementing the ADT List

23 23 Using a Vector Fig. 5-7 A client uses the methods given in ListInterface, but the implementation of the list uses Vector methods to perform its operations

24 24 Using a Vector Elements of the class Class Vector comes from package java.util Data field entry is an instance of a Vector import java.util.Vector; public class VectorList implements ListInterface { private Vector entry; // entries in list...

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

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

27 27 Java Class Library Has a class similar to class AList defined in this chapter Class ArrayList Uses dynamic array expansion Interface Serializable Represent an object as a sequence of bytes to be written to a file Add the words implements Serializable to class definition public class AList implements ListInterface, Serializable {...

28 28 Serializable This is an interface with no methods. Read the ADT specification. Read the last part of Appendix C of our text.


Download ppt "List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation."

Similar presentations


Ads by Google