I am getting some error in this program and i am not able to fix it. The problem
ID: 3604223 • Letter: I
Question
I am getting some error in this program and i am not able to fix it. The problem is with get (i)
public default boolean addAll(Collection otherList ) {
// Left as an assignment
// return true if the list changed as a result of the call
Full Code:
import java.util.Collection;
// Collection interface info:
// https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
public interface MyList<E> extends Collection<E>{
/** 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
for (int i=0; i<otherList.size(); i++) {
add(otherList.get(i));
}
return !otherList.isEmpty();
}
@Override
public default boolean removeAll(Collection<?> c ){
// Left as an assignment
// Returns true if this list changed as a result of the call
Collection<E> otherList = null;
boolean returnValue = false;
for (int i=0; i<otherList.size(); i++) {
while (remove(otherList.get(i))) {
returnValue = true;
}
}
return returnValue;
}
/** 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
Collection<E> otherList = null;
boolean returnValue = false;
for (int i=0; i<size(); i++) {
while (!otherList.contains(get(i))) {
remove(i);
returnValue = true;
}
}
return returnValue;
}
}
for (int i=0; i
add(otherList.get(i));
}
return !otherList.isEmpty();
}
@Override
public default boolean removeAll(Collection c ){
// Left as an assignment
// Returns true if this list changed as a result of the call
Collection otherList = null;
boolean returnValue = false;
for (int i=0; i
while (remove(otherList.get(i))) {
returnValue = true;
}
}
return returnValue;
}
Explanation / Answer
The issue i solved in collection interface there is no get() method so i have made use of List sub-interface which contains get(). I here with enclosing complete code
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
// Collection interface info:
// https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
public interface MyList<E> extends Collection<E>{
/** 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
List<E> listDetails=new ArrayList<E>(otherList);
for (int i=0; i<otherList.size(); i++) {
add(listDetails.get(i));
}
return !otherList.isEmpty();
}
@Override
public default boolean removeAll(Collection<?> c ){
// Left as an assignment
// Returns true if this list changed as a result of the call
//Collection<E> otherList = null;
List<Object> listDetails=new ArrayList<Object>(c);
boolean returnValue = false;
for (int i=0; i<size(); i++) {
while (remove(listDetails.get(i))) {
returnValue = true;
}
}
return returnValue;
}
/** 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
Collection<E> otherList = null;
boolean returnValue = false;
for (int i=0; i<size(); i++) {
while (!otherList.contains(get(i))) {
remove(i);
returnValue = true;
}
}
return returnValue;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.