Question: Consider the operation: removeAll - removes all elements from the list that are equal to the argument element and returns an int indicating how many
Consider the operation: removeAll - removes all elements from the list that are equal to the argument element and returns an int indicating how many elements were removed. Design a method to be added to the ArrayUnsortedList class that implements the operation. Code your method and provide a test for your method.
public class ArrayUnsortedList
// set by find method protected boolean found; // true if element found, otherwise false protected int location; // indicates location of element if found
public ArrayUnsortedList() { list = (T[]) new Object[DEFCAP]; origCap = DEFCAP; }
public ArrayUnsortedList(int origCap) { list = (T[]) new Object[origCap]; this.origCap = origCap; }
public int removeAll(T element) { int count = 0; while (remove(element)) count ++; return count;
}
protected void enlarge() // Increments the capacity of the list by an amount // equal to the original capacity. { // Create the larger array. T[] larger = (T[]) new Object[list.length + origCap];
// Copy the contents from the smaller array into the larger array. for (int i = 0; i < numElements; i++) { larger[i] = list[i]; }
// Reassign list reference. list = larger; }
protected void find(T target) // Searches list for an occurrence of an element e such that // e.equals(target). If successful, sets instance variables // found to true and location to the array index of e. If // not successful, sets found to false. { location = 0; found = false;
while (location < numElements) { if (list[location].equals(target)) { found = true; return; } else location++; } }
public void add(T element) // Adds element to this list. { if (numElements == list.length) enlarge(); list[numElements] = element; numElements++; }
public boolean remove (T element) // Removes an element e from this list such that e.equals(element) // and returns true; if no such element exists, returns false. { find(element); if (found) { list[location] = list[numElements - 1]; list[numElements - 1] = null; numElements--; } return found; }
public int size() // Returns the number of elements on this list. { return numElements; }
public boolean contains (T element) // Returns true if this list contains an element e such that // e.equals(element); otherwise, returns false. { find(element); return found; }
public T get(T element) // Returns an element e from this list such that e.equals(element); // if no such element exists, returns null. { find(element); if (found) return list[location]; else return null; }
public String toString() // Returns a nicely formatted string that represents this list. { String listString = "List: "; for (int i = 0; i < numElements; i++) listString = listString + " " + list[i] + " "; return listString; }
public void reset() // Initializes current position for an iteration through this list, // to the first element on this list. { currentPos = 0; }
public T getNext() // Preconditions: The list is not empty // The list has been reset // The list has not been modified since the most recent reset // // Returns the element at the current position on this list. // If the current position is the last element, it advances the value // of the current position to the first element; otherwise, it advances // the value of the current position to the next element. { T next = list[currentPos]; if (currentPos == (numElements - 1)) currentPos = 0; else currentPos++; return next; } }
----------------------------------------------------------------------------------------------------
package ch06.lists;
public interface ListInterface
void add(T element); // Adds element to this list.
boolean remove (T element); // Removes an element e from this list such that e.equals(element) // and returns true; if no such element exists, returns false. boolean contains (T element); // Returns true if this list contains an element e such that // e.equals(element); otherwise, returns false. T get(T element); // Returns an element e from this list such that e.equals(element); // if no such element exists, returns null. String toString(); // Returns a nicely formatted string that represents this list. void reset(); // Initializes current position for an iteration through this list, // to the first element on this list.
T getNext(); // Preconditions: The list is not empty // The list has been reset // The list has not been modified since the most recent reset // // Returns the element at the current position on this list. // If the current position is the last element, then it advances the value // of the current position to the first element; otherwise, it advances // the value of the current position to the next element. }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
