ADT Implementation Array Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1
Outline ADT Implementation using fixed size Array ADT Implementation using Array resizing Pros and Cons 2
ADT We have defined ADT called Bag and an interface called BagInterface that can contain objects of generic type 3
public interface BagInterface<T> { public int getcurrentsize(); public boolean isfull(); public boolean isempty(); public boolean add(t newentry); public T remove(); public boolean remove(t anentry); public void clear(); public int getfrequencyof(t anentry); public boolean contains(t anentry); public T[] toarray(); } // end BagInterface 4
Implementing BagInterface What is required? We have to define a class that implements the BagInterface Give implementations for all the abstract methods Define data fields Define its own new methods if necessary Lets use arrays 5
A classroom that contains desks in fixed positions 6
Implementing BagInterface Adding Arbitrarily specify consecutively numbered desks be occupied When desk #29 occupied, room is full Removing What to do when someone in middle of sequence is removed? Move last person there or shift everyone? Data fields Array of objects in the bag Maximum capacity Number of objects 7
Implementing 8
Implementing 9
Implementing Constructor A constructor for this class must create the array bag. public ArrayBag(int capacity) { numberofentries = 0; @SuppressWarnings("unchecked") T[] tempbag = (T[])new Object[capacity];// unchecked cast bag = tempbag; } // end public ArrayBag() { this(default_capacity); } 10
Design Decisions When array bag is partially full Which array elements should contain entries? Options Start at element 0 or element 1? Require elements to be sequential? 11
Add Method 12
Add method 13
isfull Method 14
toarray Method Should toarray return the array bag or a copy? Best to return a copy why? 15
isempty() More Methods getcurrentsize() 16
getfrequencyof More Methods 17
More Methods contains 18
More Methods Remove unspecified entry clear public void clear() { while(!isempty()) remove(); } 19
More Methods What about removing a given entry? E.g we want to remove Alice Two alternatives 20
More Methods Remove then reshuffle 21
More Methods Remove then move the last entry 22
More Methods Pseudocode Search the array bag for anentry If(anEntry is in the bag at bag[index]) { } Else Decrement the counter numberofentries Bag[index]=bag[numberofEntries] Bag[numberofEntries]=null Return true return false 23
More Methods private T removeentry(int givenindex) { T result = null; if (!isempty()&&(givenindex>=0)) { result = bag[givenindex]; numberofentries--; bag[givenindex]=bag[numberofentries]; bag[numberofentries]=null; } return result: } 24
More Methods 25
More Methods 26
contains More Methods 27
Using Array Resizing Array has fixed size. What if the bag is full? Resizing the array? Copy the content the array to a larger array Make the original array name refers to the larger array 28
Using copyof() int myarray={10,20,30,40,50}; myarray = Arrays.copyOf(myArray, 2 * myarray.length) 29
A New Implementation of a Bag Change name of class to ResizableArrayBag, distinguish between implementations. Remove modifier final from declaration of array bag to enable resizing. Change the name of constant DEFAULT_CAPACITY to DEFAULT_INITIAL_CAPACITY. 30
A New Implementation of a Bag Revise definition of method add to always accommodate new entry. Method will never return false. Revise definition of method isfull to always return false. A bag will never become full. 31
A New Implementation of a Bag New add method Assumes method ensurecapacity 32
Advantage Easier to implement Operations are fast Disadvantage Memory wastage Pros and Cons If the number of elements is less than the size of the array Resizing is expensive 33