import java.util.Iterator; import java.util.NoSuchElementException; /**Realization of a set by means of a SORTED extendible array */ public class ArraySet2> implements Set { private E[] A; //array storing the elements of the set private int capacity=16; //initial length of array A private int size=0; //number of elements stored in the list /**Creates the list with initial capacity 16. */ public ArraySet2(){ A= (E[]) new Comparable[capacity];//Compiler may warn, but ok } public int size() { return size; } /**Adds the specified element to this set if not already present */ //adds element in sorted order using binary search public boolean add(E e) { if(isEmpty()) addBefore(0,e); else{ int L=0; int H=size()-1; int mid=0; while(L<=H){ //System.out.println("H is " + H + " and L is " +L); mid=(H+L)/2; if (A[mid].compareTo(e)==0) return false; else{ if (A[mid].compareTo(e)<0) L=mid+1; else H=mid-1; } } addBefore (L,e); } return true; } //helper method /**add new element e at position i * and move other elements up*/ public void addBefore(int j, E e){ //System.out.println("Adding " + e + " to position " + j); if(size==capacity){ capacity *=2; E[] B=(E[]) new Comparable[capacity]; for (int i=0;ij;k--) A[k]=A[k-1]; A[j]=e; } size++; //System.out.println("added " + e + " to set"); //System.out.println("set is now " + this); } /**Removes all of the elements from this set*/ public void clear(){ for(int i=0;i iterator() {ArraySetIterator tempIterator=new ArraySetIterator(); return (Iterator) tempIterator;} private class ArraySetIterator implements Iterator{ private int current=0; /**Returns true if the iteration has more elements*/ public boolean hasNext() {return current