I have been struggling with this problem for a full day now I am a little confus
ID: 3857792 • Letter: I
Question
I have been struggling with this problem for a full day now I am a little confused on what they are asking.
1. Define the following method in MyList and implement it in MyAbstractList.
// Adds the elements in otherList to this list.
// Returns true if this list changed as a result of the call. public boolean addAll(MyList<E> otherlist)
2. Add code to “TestMyArrayList.java” to demonstrate that this method works correctly. Display the modified list using a java.util.Iterator<E> object and a while loop.
#MyList.java
package listapi;
/**
* @author Y. Daniel Liang
*/
public interface MyList<E> extends java.lang.Iterable<E> {
/** Add a new element at the end of this list*/
public void add(E e);
/**Add a new element at the specified index in this list*/
public void add(int index, E e);
/**Clear the list*/
public void clear();
/**Return true if this list contains the element*/
public boolean contains(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(E e);
/**Return true if this list contains no elements*/
public boolean isEmpty();
/**Return the index of the last matching element in this list Return -1 if
* no match.*/
public int lastIndexOf(E e);
/**Remove the first occurrence of the element o from this list. Shift any
* subsequent elements to the left. Return true if the element is removed.*/
public boolean remove(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 Object set(int index, E e);
/** Return the number of elements in this list */
public int size();
}
#MyAbstractList.java
package listapi;
/**@author Y. Daniel Liang*/
public abstract class MyAbstractList<E> implements MyList<E> {
protected int size = 0; // The size of the list
/**Create a default list*/
protected MyAbstractList() {
}
/**Create a list from an array of objects*/
protected MyAbstractList(E[] objects) {
for (int i = 0; i < objects.length; i++) {
add(objects[i]);
}
}
@Override
/**Add a new element at the end of this list*/
public void add(E e) {
add(size, e);
}
@Override
/**Return true if this list contains no elements*/
public boolean isEmpty() {
return size == 0;
}
@Override
/**Return the number of elements in this list*/
public int size() {
return size;
}
@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 boolean remove(E e) {
if (indexOf(e) >= 0) {
remove(indexOf(e));
return true;
} else {
return false;
}
}
public boolean addAll(MyList<E> otherList) {
for (int i=0; i<otherList.size(); i++) {
add(otherList.get(i));
}
return !otherList.isEmpty();
}
public boolean removeAll(MyList<E> otherList) {
boolean returnValue = false;
for (int i=0; i<otherList.size(); i++) {
while (remove(otherList.get(i))) {
returnValue = true;
}
}
return returnValue;
}
public boolean retainAll(MyList<E> otherList) {
boolean returnValue = false;
for (int i=0; i<size(); i++) {
while (!otherList.contains(get(i))) {
remove(i);
returnValue = true;
}
}
return returnValue;
}
}
#TestMyArrayList
package listapi;
/**
* @author Y. Daniel Liang
*/
public class TestMyArrayList {
public TestMyArrayList() {
// Create a list
MyList<String> list = new MyArrayList<>();
// Add elements to the list
list.add("America"); // Add it to the list
System.out.println("(1) " + list);
list.add(0, "Canada"); // Add it to the beginning of the list
System.out.println("(2) " + list);
list.add("Russia"); // Add it to the end of the list
System.out.println("(3) " + list);
list.add("France"); // Add it to the end of the list
System.out.println("(4) " + list);
list.add(2, "Germany"); // Add it to the list at index 2
System.out.println("(5) " + list);
list.add(5, "Norway"); // Add it to the list at index 5
System.out.println("(6) " + list);
// Remove elements from the list
list.remove("Canada"); // Same as list.remove(0) in this case
System.out.println("(7) " + list);
list.remove(2); // Remove the element at index 2
System.out.println("(8) " + list);
list.remove(list.size() - 1); // Remove the last element
System.out.print("(9) " + list + " (10) ");
for (String s : list) {
System.out.print(s.toUpperCase() + " ");
}
}
}
Explanation / Answer
MyList.java:
/**
* @author Y. Daniel Liang
*/
public interface MyList<E> extends java.lang.Iterable<E> {
/** Add a new element at the end of this list */
public void add(E e);
/** Add a new element at the specified index in this list */
public void add(int index, E e);
/** Clear the list */
public void clear();
/** Return true if this list contains the element */
public boolean contains(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(E e);
/** Return true if this list contains no elements */
public boolean isEmpty();
/**
* Return the index of the last matching element in this list Return -1 if
* no match.
*/
public int lastIndexOf(E e);
/**
* Remove the first occurrence of the element o from this list. Shift any
* subsequent elements to the left. Return true if the element is removed.
*/
public boolean remove(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 Object set(int index, E e);
/** Return the number of elements in this list */
public int size();
}
MyAbstractList.java:
/** @author Y. Daniel Liang */
public abstract class MyAbstractList<E> implements MyList<E> {
protected int size = 0; // The size of the list
/** Create a default list */
protected MyAbstractList() {
}
/** Create a list from an array of objects */
protected MyAbstractList(E[] objects) {
for (int i = 0; i < objects.length; i++) {
add(objects[i]);
}
}
@Override
/**Add a new element at the end of this list*/
public void add(E e) {
add(size, e);
}
@Override
/**Return true if this list contains no elements*/
public boolean isEmpty() {
return size == 0;
}
@Override
/**Return the number of elements in this list*/
public int size() {
return size;
}
@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 boolean remove(E e) {
if (indexOf(e) >= 0) {
remove(indexOf(e));
return true;
} else {
return false;
}
}
public boolean addAll(MyList<E> otherList) {
for (int i = 0; i < otherList.size(); i++) {
add(otherList.get(i));
}
return !otherList.isEmpty();
}
public boolean removeAll(MyList<E> otherList) {
boolean returnValue = false;
for (int i = 0; i < otherList.size(); i++) {
while (remove(otherList.get(i))) {
returnValue = true;
}
}
return returnValue;
}
public boolean retainAll(MyList<E> otherList) {
boolean returnValue = false;
for (int i = 0; i < size(); i++) {
while (!otherList.contains(get(i))) {
remove(i);
returnValue = true;
}
}
return returnValue;
}
}
MyArrayList.java:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyArrayList<E> extends MyAbstractList<E> {
private E[] elements;
@SuppressWarnings("unchecked")
public MyArrayList() {
elements = (E[]) new Object[10];
size = 0;
}
@Override
public void add(int index, E e) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException();
if (size() == elements.length) {
resize();
}
// slide
for (int i = size(); i > index; i--)
elements[i] = elements[i - 1];
// add element
elements[index] = e;
size++;
}
private void resize() {
@SuppressWarnings("unchecked")
E[] newArray = (E[]) new Object[size() * 2];
System.arraycopy(elements, 0, newArray, 0, size());
elements = newArray;
}
@Override
public void clear() {
size = 0;
}
@Override
public boolean contains(E e) {
return indexOf(e) != -1;
}
@Override
public E get(int index) {
checkIndex(index);
return elements[index];
}
private void checkIndex(int index) {
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException();
}
@Override
public int indexOf(E e) {
for (int i = 0; i < size(); i++) {
if ((e == null && get(i) == null)
|| (e != null && get(i).equals(e)))
return i;
}
return -1;
}
@Override
public int lastIndexOf(E e) {
int index = -1;
for (int i = 0; i < size(); i++) {
if ((e == null && get(i) == null)
|| (e != null && get(i).equals(e)))
index = i;
}
return index;
}
@Override
public E remove(int index) {
checkIndex(index);
E element = elements[index];
// slide
for (int i = index; i < size() - 1; i++)
elements[i] = elements[i + 1];
size--;
return element;
}
@Override
public Object set(int index, E e) {
checkIndex(index);
elements[index] = e;
return get(index);
}
@Override
public Iterator<E> iterator() {
return new MyArrayListIterator(0);
}
private class MyArrayListIterator implements Iterator<E> {
private int myIndex;
private int myLastIndexReturned = -1;
public MyArrayListIterator(int index) {
myIndex = index;
}
@Override
public boolean hasNext() {
return myIndex < size();
}
@Override
public E next() {
if (!hasNext())
throw new NoSuchElementException();
myLastIndexReturned = myIndex;
E result = MyArrayList.this.get(myIndex);
myIndex++;
return result;
}
@Override
public void remove() {
if (myLastIndexReturned < 0)
throw new IllegalStateException();
MyArrayList.this.remove(myLastIndexReturned);
// removal might have shifted our current index
if (myLastIndexReturned < myIndex)
myIndex--;
myLastIndexReturned = -1;
}
}
}
TestMyArrayList.java:
public class TestMyArrayList {
public TestMyArrayList() {
// Create a list
MyList<String> list = new MyArrayList<String>();
// Add elements to the list
list.add("America");
list.add(0, "Canada");
list.add("Russia"); // Add it to the end of the list
list.add("France"); // Add it to the end of the list
list.add(2, "Germany"); // Add it to the list at index 2
list.add(5, "Norway"); // Add it to the list at index 5
System.out.println(" After adding all: ");
for (String s : list) {
System.out.print(s.toUpperCase() + " ");
}
// Remove elements from the list
list.remove("Canada"); // Same as list.remove(0) in this case
System.out.println(" After removing Canada: ");
for (String s : list) {
System.out.print(s.toUpperCase() + " ");
}
list.remove(2); // Remove the element at index 2
System.out.println(" After removing 2nd element: ");
for (String s : list) {
System.out.print(s.toUpperCase() + " ");
}
list.remove(list.size() - 1); // Remove the last element
System.out.println(" After removing last element: ");
for (String s : list) {
System.out.print(s.toUpperCase() + " ");
}
}
public static void main(String args[]) {
new TestMyArrayList();
}
}
Sample Output:
After adding all:
CANADA AMERICA GERMANY RUSSIA FRANCE NORWAY
After removing Canada:
AMERICA GERMANY RUSSIA FRANCE NORWAY
After removing 2nd element:
AMERICA GERMANY FRANCE NORWAY
After removing last element:
AMERICA GERMANY FRANCE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.