// Objects should encapsulate state (fields) and expose behavior (methods) // Note that we did not comment any of these methods! We will talk in Thursdays // section and Friday's lectures about commenting in depth! public class ArrayIntList { // Absolutely remember to keep all fields private. We don't want // a client to have to "go into the circuitry" of our object to make // it work. private int[] elementData; private int size; public ArrayIntList() { // We fairly arbitrarily chose the internal array to be able to // hold up to 10 elements elementData = new int[10]; } public void add(int value) { elementData[size] = value; size++; } // Note that after we made the size field private, we still wanted a client // to be able to ask what the current size is. We can let them see the value // but not change it with this getter method, size public int size() { return size; } public int get(int index) { return elementData[index]; } public int indexOf(int value) { // One bug we had was looping to "elementData.length". Remember // that the client only "sees" the first "size" elements. Checking // the entire array would result in the client "seeing" values that // they aren't aware of for (int i = 0; i < size; i++) { if (elementData[i] == value) { return i; } } return -1; } }