Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MyList.java contains MyList interface. The implementations of the default method

ID: 3604198 • Letter: M

Question

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 don’t 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.

public default Object[] toArray(){

// Left as an assignment

//Returns an array containing all of the elements in this list.

}

@Override

public default <T> T[] toArray(T[] array){

// Left as an assignment

// Returns an array containing all of the elements in this list; the returned array

// is the specified array.

.

Explanation / Answer

Given below are the 2 methods. Without the complete interface definition, it was difficult to write the methods. I have assumed, you have methods named size() and get(int i) in the interface and used them to complete the needed methods. In case of any issues , post a comment and I'll help. But I'll need the complete interface definition for that. If the answer helped, please don't forget to rate. Thank you.

public default Object[] toArray(){
int s = size();
Object[] arr = new Object[s];
for(int i = 0; i < s; i++)
arr[i] = get(i);
return arr;
}


public default <T> T[] toArray(T[] array){
int s = size();
for(int i = 0; i < s; i++)
array[i] = (T)get(i);
return array;
}