Question
Q. MyList.java contains MyList interface. The implementations of the default methods addAll, removeAll, retainAll, toArray(), and toArray(T[]) are omitted in the MyList interface. Please implement
Q. MyList.java contains MyList interface. The implementations of the default methods addAll, removeAll, retainAll, toArray(), and toArray(T[]) are omitted in the MyList interface. Please implement these methods. MyArrayList class implements MyList interface and inherits those default methods even though you dont implement them in MyArrayList.java file. Note that you can utilize other methods defined in the MyList interface to implement those methods. Since those methods should be implemented beforehand in MyList.java, the MyArrayList.java file should not be modified. For actual files you can visit https://github.com/ubcweicai/DataStructure/tree/master/src/Assignment3 .
MyArraylist.java (It does not need any changes) but it is followed by MyList.java which need real changes.
import java.util.Collection;
public class MyArrayList
public static final int INITIAL_CAPACITY = 16;
private E[] data = (E[])(new Object[INITIAL_CAPACITY]);
private int size = 0; // Number of elements in the list
// Create an empty list
public MyArrayList(){
// no-arg constructor
}
public MyArrayList(E[] objects){
for (int i = 0; i < objects.length; i++)
add(objects[i]);
}
@Override /** Add a new element at the specified index */
public void add(int index, E e){
//Ensure the index is in the right range
checkIndex(index);
ensureCapacity();
//Move the elements to the right after the specified index
for (int i=size-1;i>=index;i--){
data[i+1] = data[i];
}
// Insert new element to data[index]
data[index] = e;
size++;
}
private void ensureCapacity(){
if (size>=data.length){
E[] newData = (E[])(new Object[size*2+1]);
System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
}
@Override // clear the list
public void clear(){
data = (E[])(new Object[INITIAL_CAPACITY]);
size = 0;
}
@Override // Return true if this list contains the element
public boolean contains(Object e){
for (int i = 0; i if (e.equals(data[i])) return true; return false; } @Override /** Return the element at the specified index**/ public E get(int index){ checkIndex(index); return data[index]; } private void checkIndex(int index){ if (index < 0 || index > size){ throw new IndexOutOfBoundsException("Index:" + index + ", Size:" +size); } } @Override /** Return the index of the first matching element in this list. * Return -1 if no match */ public int indexOf(Object e){ for(int i=0;i if (e.equals(data[i])) return i; return -1; } @Override /** Return the index of the last matching element in this list. * Return -1 if no match. */ public int lastIndexOf(E e){ for(int i = size -1; i>=0; i--) if (e.equals(data[i])) return i; return -1; } @Override /** Remove the element at the specified position in this list. * Shift any sbsequent elements to the left. * Return the element that was removed from the list. */ public E remove(int index){ checkIndex(index); E e = data[index]; //Shift data to the left for(int j = index; j data[j] = data[j+1]; data[size-1] = null; //This element is now null //Decrement size size--; return e; } @Override /** Replace the element at the specified position * in this list with the specified element. */ public E set(int index, E e){ checkIndex(index); E old = data[index]; data[index] = e; return old; } @Override public String toString(){ StringBuilder result = new StringBuilder("["); for (int i = 0; i result.append(data[i]); if (i } return result.toString() + "]"; } /** Trims the capacity to current size*/ public void trimTosize(){ if(size !=data.length){// if size == capacity, no need to trim E[] newData = (E[])(new Object[size]); System.arraycopy(data, 0, newData, 0, size); data= newData; } } @Override /** Override iterator() defined in Iterable */ public java.util.Iterator return new ArrayListIterator(); } private class ArrayListIterator implements java.util.Iterator private int current = 0; // Current index @Override public boolean hasNext(){ return current < size; } @Override public E next(){ return data[current++]; } @Override // Remove the element returned by the last next() public void remove(){ if (current == 0){// next() has not been called yet. throw new IllegalStateException(); } MyArrayList.this.remove(--current); } } @Override /** Return the number of elements in this list */ public int size(){ return size; } /** ignore the code below @Override public boolean removeAll(Collection> c){ throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection> c){ throw new UnsupportedOperationException(); } @Override public Object[] toArray(){ throw new UnsupportedOperationException(); } @Override public // Left as an exercise throw new UnsupportedOperationException(); } */ } MyList.java (It needs changes) import java.util.Collection; // Collection interface info: // https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html public interface MyList /** Add a new element at the specified index in this list */ public void add(int index, E e); /** Return the element from this list at the specified index */ public E get(int index); /** Return the index of the first matching element in this list. * Return -1 if no match. */ public int indexOf(Object e); /** Return the index of the last matching element in this list * Return -1 if no match. */ public int lastIndexOf(E e); /** Remove the element at the specified position in this list * Shift any subsequent elements to the left. * Return the element that was removed from the list. */ public E remove(int index); /** Replace the element at the specified position in this list * with the specified element and returns the new set. */ public E set(int index, E e); @Override /** Add a new element at the end of this list */ public default boolean add(E e){ add(size(), e); return true; } @Override /** Return true if this list contains no elements */ public default boolean isEmpty(){ return size() == 0; } @Override /** Remove the first occurrence of the element e * from this list. Shift any subsequent elements to the left. * Return true if the element is removed. */ public default boolean remove(Object e){ if (indexOf(e) >= 0){ remove(indexOf(e)); return true; } else return false; } @Override public default boolean containsAll(Collection> c) { for (Object e: c) if (!this.contains(e)) return false; return true; } /** Adds the elements in otherList to this list. * Returns true if this list changed as a result of the call */ @Override public default boolean addAll(Collection extends E> otherList) { // Left as an assignment // return true if the list changed as a result of the call } @Override public default boolean removeAll(Collection> c){ // Left as an assignment // Returns true if this list changed as a result of the call } /** Retains the elements in this list that are also in otherList * Returns true if this list changed as a result of the call */ @Override public default boolean retainAll(Collection> c){ // Left as an assignment // Returns true if this list changed as a result of the call } @Override public default Object[] toArray(){ // Left as an assignment //Returns an array containing all of the elements in this list. } @Override public default // Left as an assignment // Returns an array containing all of the elements in this list; the returned array // is the specified array. } }
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