/** The ArrayIntList class stores an ordered list of integers. * @author Adam Blank */ import java.util.*; public class ArrayIntList implements IntList, Iterable { private int size; private int[] data; /* DEFAULT_CAPACITY = 5 ArrayIntList list = new ArrayIntList(); list.add(5); list.add(2); list.add(17); CLIENT: [5, 2, 17] IMPLEMENTOR: [5, 2, 17, 0, 0] * */ public int numberOfEvens() { int result = 0; /* for (int i = 0; i < this.size; i++) { if (this.data[i] % 2 == 0) { result++; } } Iterator it = this.iterator(); while (it.hasNext()) { if (it.next() % 2 == 0) { result++; } } */ for (int i : this) { if (i % 2 == 0) { result++; } } return result; } public static final int DEFAULT_CAPACITY = 100000; private class ArrayIntListIterator implements Iterator { private int last; public ArrayIntListIterator() { this.last = -1; } public boolean hasNext() { return (this.last + 1) < ArrayIntList.this.size; } public Integer next() { this.last++; return ArrayIntList.this.data[this.last]; } public void remove() { throw new UnsupportedOperationException(); } } /** Initializes a new empty list with initial capacity of DEFAULT_CAPACITY integers. */ public ArrayIntList() { this.data = new int[DEFAULT_CAPACITY]; this.size = 0; } /** Returns a new iterator for the list */ public Iterator iterator() { return new ArrayIntListIterator(); } public boolean search(int value) { for (int i = 0; i < this.size(); i++) { if (this.get(i) == value) { return true; } } return false; } /** * Appends value to the end of the list.

* post: value is appended to the ArrayList */ public void add(int value) { this.grow(this.size); this.data[this.size] = value; this.size++; } /** * Returns the value at index.

* pre: 0 <= index <= size otherwise throws IndexOutOfBoundsException */ public int get(int index) { checkIndex(index); return this.data[index]; } /** * Sets the data at index to value.

* pre: 0 <= index <= size otherwise throws IndexOutOfBoundsException */ public void set(int index, int value) { checkIndex(index); if (index == this.size) { this.size++; } this.data[index] = value; } /** * Removes the value at the given index, shifting later values over.

* pre: 0 <= index < size otherwise throws IndexOutOfBoundsException */ public void remove(int index) { checkIndex(index); for (int i = index; i < this.size - 1; i++) { this.set(i, this.get(i+1)); } this.size--; } /** Returns the number of elements in the list. */ public int size() { return this.size; } /** * Returns a String representation of the list consisting of the elements * in order, separated by commas and enclosed in square brackets. */ public String toString() { String result = ""; if (this.size > 0) { result = "" + this.data[0]; for (int i = 1; i < this.size; i++) { result += ", " + this.data[i]; } } return "[" + result + "]"; } /** * Grows the ArrayIntList to have room for *at least* requiredCapacity elements.

* pre: 0 <= requiredCapacity otherwise throws IllegalArgumentException */ private void grow(int requiredCapacity) { if (requiredCapacity < 0) { throw new IllegalArgumentException(); } if (requiredCapacity > this.data.length) { this.data = Arrays.copyOf(this.data, this.data.length * 2); } } /** * Checks if index is a valid index into this ArrayIntList.

* post: throws IndexOutOfBoundsException if index < 0 or index >= max */ private void checkIndex(int index) { if (0 > index || index >= this.size) { throw new IndexOutOfBoundsException(); } } }