1 / 28

List Implementations That Use Arrays

List Implementations That Use Arrays. Chapter 5. 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

ziarre
Download Presentation

List Implementations That Use Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 • 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 InterfaceSerializable

  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. An Analogy Fig. 5-1 A classroom that contains desks in a fixed position.

  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. Adding a Student Fig. 5-2 Seating a new student between two existing students: at least one other student must move

  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. AList Implementation (1): public class AList<T> implements ListInterface<T> { 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) { /* < Implementation deferred > */ } // end add

  9. AList Implementation (2): public boolean add(int newPosition, T newEntry) { /* < Implementation deferred > */ } // end add public T remove(int givenPosition) { /* < Implementation deferred > */ } // end remove public void clear() { length = 0; //< But see Question 8. > } // end clear public boolean replace(int givenPosition, T newEntry) { /* < Implementation deferred > */ } // end replace public T getEntry(int givenPosition) { /* < Implementation deferred > */ } // end getEntry

  10. AList Implementation (3): public boolean contains(T anEntry) { /* < Implementation deferred > */ } // 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. 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. 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. Adding Items in Mid-list Fig. 5-3 Making room to insert Carla as third entry in an array.

  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. Removing a List Entry Fig. 5-4 Removing Bob by shifting array entries.

  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. Dynamic Array Expansion Fig. 5-5 The dynamic expansion of an array copies the array's contents to a larger second array.

  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. 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. 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. A new add() method (2): /** 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 • NOTE: See System.arraycopy() for built-in array copy method.

  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. 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. 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. 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. 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. 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 implementsSerializable to class definition public class AList implements ListInterface, Serializable{ . . .

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

More Related