//implantation identique à ce qui est dans le package WattBrown; //Version générique juin 2010 import java.util.Iterator; public class ArrayList implements List { // Each ArrayList object is an indexed list (sequence) whose elements // are objects. // This list is represented as follows: its elements occupy // elems[0..length-1]. protected Object[] elems; protected int length; // ////////// Constructor //////////// public ArrayList(int maxlength) { // Construct a list, initially empty, whose length will be bounded by // maxlength. elems = new Object[maxlength]; length = 0; } public ArrayList() { this(10); } protected ArrayList(ArraySet that){ this.elems=that.elems; this.length=that.length; } public String toString() { StringBuilder sb = new StringBuilder("["); for (Iterator i = iterator(); i.hasNext();) { sb.append(i.next()); if (i.hasNext()) sb.append(", "); } return sb + "]"; } // ////////// Accessors //////////// public boolean isEmpty() { // Return true if and only if this list is empty. return (size() == 0); } public int size() { // Return this list's length. return length; } @SuppressWarnings("unchecked") public E get(int i) { // Return the element with index i in this list. Throw an // IndexOutOfBoundsException if i is out of range. if (i < 0 || i >= length) throw new IndexOutOfBoundsException("index:" + i + " negative or greater than " + length); return (E) elems[i]; } public boolean equals(List that) { // Return true if and only if this list and that have the same // length, and each element of this list equals the corresponding // element of that. if (length != that.size()) return false; Iterator it = that.iterator(); for (E e : this) if (!e.equals(it.next())) return false; return true; } @SuppressWarnings("unchecked") public boolean equals(Object that) { if (that == null) return false; if (!(that instanceof List)) return false; return equals((List) that); } public int hashCode() { int h = 1; for (E e : this) if (e != null) h = h * 31 + e.hashCode(); return h; } public boolean contains(E elem) { return indexOf(elem) >= 0; } public int indexOf(E elem) { for (int i = 0; i < length; i++) if (elem.equals(get(i))) return i; return -1; } // ////////// Transformers //////////// public void clear() { // Make this list empty. for (int i = 0; i < length; i++) elems[i] = null; length = 0; } public E set(int i, E elem) { // Replace by elem the element at index i in this list. Throw an // IndexOutOfBoundsException if i is out of range. E old = get(i); elems[i] = elem; return old; } public boolean add(E elem) { // Add elem after the last element of this list. if (length == elems.length) expand(); elems[length++] = elem; return true; } public boolean add(int i, E elem) { // Add elem as the element with index i in this list. Throw an // IndexOutOfBoundsException if i is out of range. if (i < 0 || i > length) throw new IndexOutOfBoundsException("index:" + i + " negative or greater than " + length); if (length == elems.length) expand(); for (int j = length; j > i; j--) elems[j] = elems[j - 1]; elems[i] = elem; length++; return true; } public boolean addAll(List that) { // Add all the elements of that after the last element of this list. if(that.isEmpty())return false; for (E e : that) add(e); return true; } public E remove(int i) { // Remove and return the element with index i in this list. Throw an // IndexOutOfBoundsException if i is out of range. E oldElem = get(i); System.arraycopy(elems, i + 1, elems, i, length - i - 1); // for (int j = i+1; j < length; j++) // elems[j-1] = elems[j]; length--; elems[length] = null; return oldElem; } // ////////// Iterator //////////// public Iterator iterator() { // Return an iterator that will visit all elements of this list, // in left-to-right order. return new LRIterator(); } // ////////// Auxiliary method //////////// protected void expand() { // Make the elems array longer. Object[] newElems = new Object[2 * length]; System.arraycopy(elems, 0, newElems, 0, length); elems = newElems; } // ////////// Inner class //////////// protected class LRIterator implements Iterator { // An ArrayList.LRIterator object is a left-to-right iterator over a // list represented by an ArrayList object. // This iterator is represented by the array index of the next // element to be visited, place. private int place; LRIterator() { place = 0; } public boolean hasNext() { return (place < length); } public E next() { return get(place++); } public void remove() { ArrayList.this.remove(--place); } } }