Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Iterator; import java.util.NoSuchElementException; public class Quiz3aWrapper { public static interface List extends Iterable { public int size(); public boolean isEmpty(); public boolean isMember(E e);

image text in transcribed

import java.util.Iterator; import java.util.NoSuchElementException;

public class Quiz3aWrapper {

public static interface List extends Iterable { public int size(); public boolean isEmpty(); public boolean isMember(E e); public int firstIndexOf(E e); public int lastIndexOf(E e);

public void add(E e); public void add(E e, int position); public E get(int position); public E remove(int position); public E replace(int position, E newElement); public void clear(); public Object[] toArray(); public int removeDuplicates();

}

@SuppressWarnings("unchecked")

public static class ArrayList implements List { @SuppressWarnings("hiding") private class ArrayListIterator implements Iterator { private int currentPosition;

public ArrayListIterator() { super(); this.currentPosition = 0; }

@Override public boolean hasNext() { return this.currentPosition

@Override public E next() { if (this.hasNext()) { E result = (E) elements[this.currentPosition++]; // elements is array in enclosing class return result; } else { throw new NoSuchElementException(); } } } private E[] elements; private int currentSize; private static final int DEFAULT_SIZE = 10; public ArrayList(int initialSize) { if (initialSize

public ArrayList() { this(DEFAULT_SIZE); } @Override public int size() { return this.currentSize; }

@Override public boolean isEmpty() { return this.size() == 0; }

@Override public boolean isMember(E e) { return this.firstIndexOf(e) >= 0; }

@Override public int firstIndexOf(E e) { for (int i=0; i

@Override public void add(E e) { if (this.size() == this.elements.length) { this.reAllocate(); } this.elements[this.currentSize++] = e; }

private void reAllocate() { E[] temp = (E[]) new Object[2*this.size()]; for (int i=0; i

@Override public void add(E e, int position) { if ((position this.currentSize)){ throw new IndexOutOfBoundsException("Illegal position"); } if (position == this.currentSize) { this.add(e); } else { if (this.size() == this.elements.length) { this.reAllocate(); } for (int i=this.currentSize; i > position; --i) { this.elements[i] = this.elements[i-1]; } this.elements[position] = e; this.currentSize++; } }

@Override public E get(int position) { if ((position = this.currentSize)) { throw new IndexOutOfBoundsException("Illegal position"); } return this.elements[position]; }

@Override public E remove(int position) { if ((position = this.currentSize)) { throw new IndexOutOfBoundsException("Illegal position"); } E result = this.elements[position];

for (int i=position; i

}

@Override public E replace(int position, E newElement) { if ((position = this.currentSize)) { throw new IndexOutOfBoundsException("Illegal position"); } E result = this.elements[position]; this.elements[position] = newElement; return result; }

@Override public void clear() { while(!this.isEmpty()) { this.remove(0); } }

@Override public Object[] toArray() { Object[] result = (E[]) new Object[this.size()]; System.arraycopy(this.elements, 0, result, 0, this.size()); // for (int i=0; i

@Override public Iterator iterator() { return new ArrayListIterator(); }

@Override public int lastIndexOf(E e) { for (int i=this.currentSize-1; i>= 0; --i) { if (this.elements[i].equals(e)) { return i; } } // not found return -1; }

@Override public int removeDuplicates() { int counter=0; for(int i=0;i Implement a member method for the List ADT called remove Duplicates. This method removes all duplicate elements from a list but keeps the relative order of the remaining elements. The method returns the number of copies removed. Implement this method for the ArrayList class For example, if L = {Jim, Joe, Jim, Ned, Ned), then a call to remove Duplicates 0, make L = (Jim, Joe, Ned), and returns 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions

Question

What is conservative approach ?

Answered: 1 week ago

Question

What are the basic financial decisions ?

Answered: 1 week ago