addAll at index, iterator set methods
Based on the lectures this week, we implemented ArrayList -- anorder-dependent collection that grows as more data is added to it.Overall, it was based on an underlying array. However, two methodswere left unimplemented: ArrayList.addAll(int index, Collection)and ArrayListIterator.set(E element). In this problem you are toimplement those methods. A suite of test cases is provided.Consulting the documentation for List and ListIterator may behelpful.
You should not need access to any private fields, but based onthe presentation in class, the following data and methods have beenmade protected instead of private so that you may access them:
public class ArrayList extends AbstractList {
protected static final int DEFAULT_CAPACITY = 10;
protected E [] data;
protected int size;
protected int modCount;
protected void ensureCapacity(int requiredCapacity) {
// etc. }
protected class ArrayListIterator implements ListIterator {
protected int cursor;
protected int priorCursor;
protected int expectedModCount;
protected void checkForComodification() {
// etc. }
protected void updateAndSynchronizeModeCounts() {
// etc }
}
}
import java.util.Collection;import java.util.List;import java.util.Iterator;import java.util.ListIterator;
public class W7Problem<E> extends ArrayList<E>{ // Scaffolding for the problem. Don't touch. public W7Problem() { super(); }
// Scaffolding for the problem. Don't touch. public W7Problem(int initialCapacity) { super(initialCapacity); }
/** * Inserts all the elements of<tt>coll</tt> into this collection at * the specified location. If both thiscollection and the parameter * are the same collection, then the operationalbehavior is undefined * (i.e. bad things can happen). * @param index the location at which toinsert * @param coll the collection from which to drawelements for addition. * @return true when this collection is modifiedas a result. * * Time complexity: TODO___________ */ @Override public boolean addAll(int index, Collection<?extends E> coll) { // TODO: your code goes here throw newUnsupportedOperationException(); }
// Scaffolding for the problem. Don't touch. @Override public Iterator<E> iterator() { return listIterator(); }
// Scaffolding for the problem. Don't touch. @Override public ListIterator<E> listIterator() { return listIterator(0); }
// Scaffolding for the problem. Don't touch. @Override public ListIterator<E> listIterator(int index){ return newW7ProblemIterator(index); }
private class W7ProblemIterator extendsArrayList<E>.ArrayListIterator { // Scaffolding for the probmem. Don'ttouch. public W7ProblemIterator(intstartIndex) { super(startIndex); }
/** * Replaces the last elementreturned by either <tt>next</tt> or * <tt>previous</tt>. Cannot be called immediately after<tt>add</tt> * or<tt>remove</tt>. * @param obj the object withwhich to replace the one returned * earlier. * * Time complexity:TODO___________ */ @Override public void set(E obj) { // TODO: Your code goeshere throw newUnsupportedOperationException(); } }}
addAll at index, iterator set methods Based on the lectures this week, we implemented ArrayList -- an order-dependent co
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am