Question: import java.util.Iterator; import java.util.NoSuchElementException; public class KeepOddWrapper { public interface List extends Iterable { public int size ( ) ; public boolean isEmpty ( )
import java.util.Iterator;
import java.util.NoSuchElementException;
public class KeepOddWrapper
public interface List extends Iterable
public int size;
public boolean isEmpty;
public boolean isMemberE e;
public int firstIndexOfE e;
public int lastIndexOfE e;
public void addE e;
public void addE e int position;
public E getint position;
public E removeint position;
public E replaceint position, E newElement;
public void clear;
public Object toArray;
public int keepOdd;
@SuppressWarningsunchecked
public static class ArrayList implements List
@SuppressWarningshiding
private class ArrayListIterator implements Iterator
private int currentPosition;
public ArrayListIterator
super;
this.currentPosition ;
@Override
public boolean hasNext
return this.currentPosition currentSize;
@Override
public E next
if thishasNext
E result E elementsthiscurrentPosition; elements is array in enclosing class
return result;
else
throw new NoSuchElementException;
private E elements;
private int currentSize;
private static final int DEFAULTSIZE ;
public ArrayListint initialSize
if initialSize
throw new IllegalArgumentExceptionSize must be at least ;
this.elements E new ObjectinitialSize;
this.currentSize ;
public ArrayList
thisDEFAULTSIZE;
@Override
public int size
return this.currentSize;
@Override
public boolean isEmpty
return this.size;
@Override
public boolean isMemberE e
return this.firstIndexOfe;
@Override
public int firstIndexOfE e
for int i; i this.size; i
if thiselementsiequalse
return i;
return ;
@Override
public void addE e
if thissize this.elements.length
this.reAllocate;
this.elementsthiscurrentSize e;
private void reAllocate
E temp E new Objectthissize;
for int i; i this.size; i
tempi this.elementsi;
this.elements temp;
@Override
public void addE e int position
if position position this.currentSize
throw new IndexOutOfBoundsExceptionIllegal position";
if position this.currentSize
this.adde;
else
if thissize this.elements.length
this.reAllocate;
for int ithis.currentSize; i position; i
this.elementsi this.elementsi;
this.elementsposition e;
this.currentSize;
@Override
public E getint position
if position position this.currentSize
throw new IndexOutOfBoundsExceptionIllegal position";
return this.elementsposition;
@Override
public E removeint position
if position position this.currentSize
throw new IndexOutOfBoundsExceptionIllegal position";
E result this.elementsposition;
for int iposition; i this.size; i
this.elementsi this.elementsi ;
this.elementsthiscurrentSize null;
this.currentSize;
return result;
@Override
public E replaceint position, E newElement
if position position this.currentSize
throw new IndexOutOfBoundsExceptionIllegal position";
E result this.elementsposition;
this.elementsposition newElement;
return result;
@Override
public void clear
whilethis.isEmpty
this.remove;
@Override
public Object toArray
Object result E new Objectthissize;
System.arraycopythiselements, result, this.size;
for int i; i this.size; i
resulti this.elementsi;
return result;
@Override
public Iterator iterator
return new ArrayListIterator;
@Override
public int lastIndexOfE e
for int ithis.currentSize; i; i
if thiselementsiequalse
return i;
not found
return ;
@Override
public int keepOdd
int removedCount ;
int currentIndex ; Start at index which is the second element odd index
Continue looping while currentIndex is within the bounds of the list
while currentIndex this.size
this.Consider a member method keepOdd for the List ADT. This method takes a list L and only keeps the elements in the odd positions ie
The method returns the number of elements in even positions thremovecurrentIndex; Remove the element at the
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
