Question
UArrayList Code: import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; public class UArrayList extends AArrayList implements UnorderedListADT { // Append element to list (at the end) //
UArrayList Code:
import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException;
public class UArrayList
// Append element to list (at the end) // allocate larger array when list is full @Override public boolean add(T element) { if (isFull()) // list is full growList();
listArray[listSize++] = element; modifiedCount++; return true; }
// Add element to front of the list // allocate larger array when list is full @Override public void addToFront(T element) { // TODO Auto-generated method stub
}
// Add element after an existing element in the list // Throw NoSuchElementException if existingElement is not found in the list @Override public void addAfter(T existingElement, T element) throws NoSuchElementException {
}
// Add element at specified index location // Throw IndexOutOfBoundsException if index is invalid public void addAt(int index, T element) throws IndexOutOfBoundsException { // TODO Auto-generated method stub
}
}
AArrayList Code:
import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException;
import exceptions.EmptyCollectionException;
public abstract class AArrayList
// Constructors // Create a new list object with maximum size "size" @SuppressWarnings("unchecked") // Generic array allocation AArrayList(int size) { // maxSize = size; listSize = modifiedCount = 0; listArray = (T[]) new Object[size]; // Create listArray }
// Create a list with the default capacity AArrayList() { this(DEFAULT_SIZE); // Just call the other constructor }
// Reset the list -- note that we could also iterate the array and null all // values public void clear() { // Reinitialize the list listSize = modifiedCount = 0; // Simply reinitialize values }
@Override // the add method will be different for each subclass public abstract boolean add(T element);
// Increase the size of the array when it becomes full protected void growList() { // create a larger array newList // copy all elements of list into newList array // update the list reference to point to newList
@SuppressWarnings("unchecked") T[] newList = (T[]) (new Object[size() + DEFAULT_SIZE]);
for (int i = 0; i
listArray = newList;
}
@Override public T remove(T element) throws NoSuchElementException { if (isEmpty()) // list is empty throw NoSuchElementException throw new NoSuchElementException(); else { // loop over the list searching for element for(int i=0;i // element not found in list, throw NoSuchElementException throw new NoSuchElementException(); } } @Override public T removeFirst() { // Remove first element from the list T retElement; if (isEmpty()) throw new EmptyCollectionException("list"); retElement = listArray[FRONT]; // shift elements for (int i = 0; i listArray[listSize] = null; // null where the last element used to be listSize--; modifiedCount++; return retElement; } @Override public T removeLast() throws NoSuchElementException { if(isEmpty()) // empty list, throw NoSuchElementException throw new NoSuchElementException(); listSize--; // decrement listSize by 1, thus removing the last element from list return listArray[listSize]; // return the last element } @Override public int size() { return listSize; // Return list size } // Tell if the list is empty or not public boolean isEmpty() { return listSize == 0; } protected boolean isFull() { return (listSize == listArray.length); } @Override public T first() { // if the list is empty, throw an exception if (isEmpty()) throw new EmptyCollectionException("list"); // returns the first element in the list return listArray[0]; } @Override public T last() { if (isEmpty()) throw new EmptyCollectionException("list"); return listArray[listSize - 1]; } @Override public boolean contains(T element) { // iterate through the list length for (int i = 0; i @Override public int indexOf(T element) { for (int i = 0; i @Override public void toArray(T[] anArray) { // TODO Auto-generated method stub } @Override public String toString() { String retString = "["; for (int i = 0; i // You may use this method to shift elements and open an index location to add protected void shiftElements(int insertingIndex) { // shift elements so that the index location at // starting index is open for the new element for (int i = size(); i > insertingIndex; i--) { listArray[i] = listArray[i - 1]; } listArray[insertingIndex] = null; } @Override public Iterator // private iterator class private class ArrayListIterator implements Iterator int currentPos; int currentModCount; public ArrayListIterator() { currentPos = 0; currentModCount = modifiedCount; } @Override public boolean hasNext() { // Indicate if there are elements left to iterate if (currentModCount != modifiedCount) { throw new ConcurrentModificationException(); } return (currentPos @Override public T next() { // Return the next element in the list // First time calling next returns the first element in the list if (hasNext()) { currentPos++; return listArray[currentPos - 1]; } else { throw new NoSuchElementException(); } } } } ListADT Code: import java.util.NoSuchElementException; public interface ListADT // Append "it" at the end of the list for unordered lists // Add at appropriate position for ordered lists // The list must ensure that the list's capacity is not exceeded public boolean add(T it); // Remove and return the current element public T remove(T element) throws NoSuchElementException; // Remove and return the first element public T removeFirst() throws NoSuchElementException; // Remove and return the last element public T removeLast() throws NoSuchElementException; // Return the number of elements in the list public int size(); // Tell if the list is empty or not public boolean isEmpty(); // Return the first element in the list public T first(); // Return the last element in the list public T last(); // Tell if the element exists in the list public boolean contains(T element); // Return the index of the specified element public int indexOf(T element); // Copies the elements of the list onto the parameter array public void toArray(T[] anArray);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started