/* Second Array-based implementation of a list */ import java.io.*; public class AList implements ListInterface, Serializable { 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 public AList() { length = 0; entry = new Object[MAX_SIZE]; } // end default constructor public AList(int maxSize) { length = 0; entry = new Object[maxSize]; } // end constructor /** Task: adds an element to the list when the list is not full but doubles the size of the list when full then * adds the element */ public boolean add(Object newEntry) { if (isArrayFull()) doubleArray(); // add new entry after last current entry entry[length] = newEntry; length++; return true; } // end add /** Task: Doubles the size of the array of list entries. */ private void doubleArray() { Object[] oldList = entry; // save reference to array of // list entries int oldSize = oldList.length; // save old max size of array entry = 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 private boolean isArrayFull() { return isFull(); } //Need to change this function to double the size of array when isFull public boolean add(int newPosition, Object newEntry) { boolean isSuccessful = true; if (isArrayFull()) doubleArray(); if ((newPosition >= 1) && (newPosition <= length+1)) { makeRoom(newPosition); entry[newPosition-1] = newEntry; length++; } else isSuccessful = false; return isSuccessful; } // end add /** Task: Makes room for a new entry at newPosition. * Precondition: 1 <= newPosition <= length+1; * length is list's length before addition. */ private void makeRoom(int newPosition) { // move each entry to next higher position, starting at end of // list and continuing until the entry at newPosition is moved for (int index = length; index >= newPosition; index--) entry[index] = entry[index-1]; } // end makeRoom public Object remove(int givenPosition) { Object result = null; // return value if ((givenPosition >= 1) && (givenPosition <= length)) { result = entry[givenPosition-1]; // get entry to be removed // move subsequent entries toward entry to be removed, // unless it is last in list if (givenPosition < length) removeGap(givenPosition); length--; } // end if return result; // return reference to removed entry, // or null if givenPosition is invalid } // end remove /** Task: Shifts entries that are beyond the entry to be removed * to next lower position. * Precondition: 1 <= givenPosition <= length; * length is listās length before removal. */ private void removeGap(int givenPosition) { for (int index = givenPosition; index < length; index++) entry[index-1] = entry[index]; } // end removeGap public boolean replace(int givenPosition, Object newEntry) { boolean isSuccessful = true; if ((givenPosition >= 1) && (givenPosition <= length)) entry[givenPosition-1] = newEntry; else isSuccessful = false; return isSuccessful; } // end replace public Object getEntry(int givenPosition) { Object result = null; // result to return if ((givenPosition >= 1) && (givenPosition <= length)) result = entry[givenPosition-1]; return result; } // end getEntry public boolean contains(Object anEntry) { boolean found = false; for (int index = 0; !found && (index < length); index++) { if (anEntry.equals(entry[index])) found = true; } // end for return found; } // end contains /** Task: Removes all entries from the list. */ public void clear() { length = 0; } public int getLength() { return length; } // end getLength public boolean isEmpty() { return length == 0; } // end isEmpty public boolean isFull() { return length == entry.length; } // end isFull public void display() { System.out.print("["); for (int index = 0; index < length-1; index++) System.out.print(entry[index] + ","); System.out.print(entry[length-1] + "]"); } // end display /* < This class will define two private methods that will be discussed later. > */ } // end AList