// CSE 143, Winter 2009, Marty Stepp // An ArrayIntList object stores an ordered list of integers using // an unfilled array. // // This is an in-progress version of the class. You should not use // this version of ArrayIntList in your Homework 1 program. Use the // version found in the Homework section of the course web site. public class ArrayIntList { // data (fields) private int[] elementData; // stores the list's elements private int size; // number of elements in the list // constructors (creates/initializes new objects) // Initializes a new empty list that can hold up to 1000 integers. public ArrayIntList() { elementData = new int[1000]; size = 0; } // behavior (methods) // Adds the given value to the end of the list. // Precondition: size < elementData.length public void add(int value) { elementData[size] = value; size++; } // Inserts the given value into the list at the given index. // Precondition: size < elementData.length and 0 <= index <= size public void add(int index, int value) { // shift elements right to make room for the new element for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } elementData[index] = value; size++; } // Returns the value in the list at the given index. // Precondition: 0 <= index < size public int get(int index) { return elementData[index]; } // Returns the number of elements in the list. public int size() { return size; } }