list implementations that use arrays
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/1.jpg)
List Implementations That Use Arrays
Chapter 5
![Page 2: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/2.jpg)
2
Chapter ContentsUsing 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
![Page 3: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/3.jpg)
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
![Page 4: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/4.jpg)
4
An Analogy
Fig. 5-1 A classroom that contains desks in a fixed position.
![Page 5: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/5.jpg)
5
An Analogy
Suppose we have some students in classroom in order alphabeticallyWe 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
![Page 6: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/6.jpg)
6
Adding a Student
Fig. 5-2 Seating a new student between two existing students: at least one other student must move
![Page 7: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/7.jpg)
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
![Page 8: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/8.jpg)
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
![Page 9: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/9.jpg)
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
![Page 10: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/10.jpg)
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
![Page 11: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/11.jpg)
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
![Page 12: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/12.jpg)
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
![Page 13: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/13.jpg)
13
Adding Items in Mid-list
Fig. 5-3 Making room to insert Carla as third entry in an array.
![Page 14: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/14.jpg)
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
![Page 15: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/15.jpg)
15
Removing a List Entry
Fig. 5-4 Removing Bob by shifting array entries.
![Page 16: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/16.jpg)
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
![Page 17: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/17.jpg)
17
Dynamic Array Expansion
Fig. 5-5 The dynamic expansion of an array copies the array's contents to a larger second array.
![Page 18: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/18.jpg)
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
![Page 19: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/19.jpg)
19
A New Implementation of a ListChange 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 fullNow declare a private method isArrayFull • Called by the add() methods
![Page 20: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/20.jpg)
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
![Page 21: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/21.jpg)
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
![Page 22: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/22.jpg)
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
![Page 23: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/23.jpg)
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
![Page 24: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/24.jpg)
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
. . .
![Page 25: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/25.jpg)
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
![Page 26: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/26.jpg)
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
![Page 27: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/27.jpg)
27
Java Class LibraryHas 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 definitionpublic class AList implements ListInterface, Serializable{ . . .
![Page 28: List Implementations That Use Arrays](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816837550346895dddf90d/html5/thumbnails/28.jpg)
28
SerializableThis is an interface with no methods. Read the ADT specification. Read the last part of Appendix C of our text.