Question: In this assignment, I have to write additional methods in the MyArrayList class. This new version of the MyArrayList class contains all the code (the
In this assignment, I have to write additional methods in the MyArrayList class. This new version of the MyArrayList class contains all the code (the code below) and several new methods in the MyCollection interface described below.
This is a list of things that I have to add to MyCollection:
public interface MyCollection extends Iterator Follow the guidelines in the Programming Standards document. methods in the MyCollection interface
public boolean removeIf(Predicate filter) - Removes all of the elements of this collection that satisfy the given predicate. Returns true if at least one element is removed from the list. Otherwise, the method returns false. public void forEach(Consumer action) performs the given action for each element of the list. (See the example in LinkedList.java). public void replaceAll(Function operator) replaces each element of this list with the result of applying the operator to that element. public void sort(Comparator c) - Sorts this list according to the order induced by the specified Comparator. Suggestion: Create a temporary array whose length = size. Copy elements 0 through size -1 of the list array to the temporary array. Use Arrays.sort to sort the temporary array. Copy the contents of the temporary array back to the list array. public MyCollection subList(int fromIndex, int toIndex) Throw an IndexOutOfBoundsException if fromIndex < 0 or toIndex > size. Throw an IllegalArgumentException if fromIndex > toIndex. Otherwise returns a portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) public boolean removeAll(MyCollection c) - Removes from this list all of its elements that are not contained in the specified collection. Returns true if at least one element is removed from the list. Otherwise, the method returns false. public boolean retainAll(MyCollection c) - Removes from this list all of its elements that are contained in the specified collection. Returns true if at least one element is removed from the list. Otherwise, the method returns false. public Iterator iterator() returns an Iterator for the MyArrayList object
-----------------------------end of the assignment-----------------------------------
I need help adding these methods to my existing code.
------------------------------------------------------------
Below is My Arral List class
----------------------------------------------------------------------
public class MyArrayList implements MyCollection {
private E[] list;
private int size;
private int capacity;
public MyArrayList(int capacity) { // constructor
list = (E[]) new Object[capacity]; // empty list
this.capacity = capacity;
}
public MyArrayList() { // constructor
this(10); // to call the first constructor with an input argument equal
// to 10
}
private void ensureCapacity(int minCapacity) { // Increases the capacity of
// the list array, if
// necessary
if (minCapacity > size) {
E[] tmp = (E[]) new Object[minCapacity];
System.arraycopy(list, 0, tmp, 0, list.length);
capacity = minCapacity;
list = tmp;
}
}
// --------------------------------------------------------
public boolean isEmpty() { // return true if size = 0
return size == 0;
}
public int size() {
return size;
}
public E get(int index) {// The method throws an IndexOutOfBoundsException if
// index < 0 or index size
// Otherwise, return the item at the specified
// position
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return list[index];}
public boolean contains(Object o) {// return true if the object is in the
// list; otherwise return false
boolean found = false;
int index = 0;
while (index < size && !found) {
if (list[index] == null) {
if (o == null) {
found = true;
}
} else {
found = list[index].equals(o);
}
index++;
}
return found;
public boolean add(E item) {// increase capacity 2 times
if (size >= capacity) {
ensureCapacity(2 * capacity);
}
// System.out.println("size = " + size);
list[size++] = item;
return true;
}
public void add(int index, E item) { // The method throws an
// IndexOutOfBoundsException if
// index
// < 0 or index > size
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
if (size >= capacity) {
ensureCapacity(2 * capacity);
}
System.arraycopy(list, index, list, index + 1, size - index);
list[index] = item;
size++;
}
public boolean remove(Object o) {// Removes the first occurrence of the
// specified element from this list
// if it is present and then returns
// true
boolean removed = false;
int index = 0;
while (index < size && !removed) {
if (list[index] == null) {
if (o == null) {
removed = true;
}
} else {
removed = list[index].equals(o);
}
if (!removed) {
index++;
}
}
if (removed) {
System.arraycopy(list, index + 1, list, index, size - index - 1);
size--;
}
return removed;
}
public int indexOf(Object o) { // returns the index of the first occurrence
// of the specified element
// this list, or -1 if this list does not
// contain the element
for (int i = 0; i < size; i++) {
if (equals(o, list[i])) {
return i;
}
}
return -1;
}
private boolean equals(Object target, Object element) {
if (target == null) {
return element == null;
} else {
return target.equals(element);
}
}
public void clear() {// Assign null to each value of list and set size to
// zero
for (int i = 0; i < size; i++) {
list[i] = null;
}
size = 0;
}
public E set(int index, E element) {
// no need to check index; get will do it for us
E old = get(index);
list[index] = element;
return old;
}
public E remove(int index) {// The method throws an
// IndexOutOfBoundsException if index < 0 or
// index size
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
} else {
E element = get(index);
for (int i = index; i < size - 1; i++) {
list[i] = list[i + 1];
}
size--;
return element; } } }
-------------------------- Below is the MyCollection interface code ----------------------------------
public interface MyCollection {
boolean remove(Object o);
int size();
T get(int i);
boolean isEmpty();
boolean add(T item);
T set(int i, T j);
T remove(int i);
void clear();
void add(int i, T item);
boolean contains(T item);
int indexOf(Object i);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
