import java.util.Arrays; public class IntegerArrayList implements IntegerList{ private int[] array; //integer data structure private int numberElements; //counts number of actual elements /* Create list with no elements */ public IntegerArrayList(){ this.numberElements = 0; this.array = new int[ 10 ]; } /* Create a list with no elements, but set the capacity to the parameter initialCapacity */ public IntegerArrayList( int initialCapacity ){ if( initialCapacity <= 0 ) throw new NegativeArraySizeException("Invalid intitial capacity of "+ initialCapacity +". Capacity must be 1 or more"); this.numberElements = 0; this.array = new int[ initialCapacity ]; } /* Create list with all elements of initList in the order 0...n-1 */ public IntegerArrayList( int[] initialData ){ this.numberElements = initialData.length; this.array = new int[ initialData.length * 2 ]; for( int i = 0; i < initialData.length; ++i ){ this.array[i] = initialData[i]; } } /* Create list with all the elements of otherList in the order 0...n-1 */ public IntegerArrayList( IntegerList otherList ){ this( otherList.size() * 2); for( int i = 0; i < otherList.size(); ++i ){ this.add( otherList.get(i)); } } /********* Private Methods *********/ /* Checks if size needs to be reallocated * Returns nothing */ private void checkSize(){ if( this.size() == this.capacity() ){ // do I need to make bigger? int[] newArray = new int[ this.capacity() * 2 ]; //allocate to twice as big as current capacity //copy current elements into newArray for( int i =0; i < this.size(); ++i ){ newArray[i] = this.get( i ); } this.array = newArray; //swap arrays. } } /* Performs element left shift in the array, starting at removeIndex * and continuing to size(). the size()'th elemtent is set to 0; * number of elements is then decremented; */ private void removeShift( int removeIndex ){ //shift elements over within array; for( int i = removeIndex; i < this.size(); ++i ){ if( i == this.size()-1 ){ // at the end of the valid array-space; note that this eliminates AIOBexceptions for size() == cap() this.array[i] = 0; } else { //usual case this.array[i] = this.array[i+1]; } } } /* Performs element right shift in the array, starting at insertIndex * and continuing to size(). the size()'th elemtent is set to 0; * number of elements is then decremented; */ private void insertShift( int insertIndex ){ this.checkSize(); //creates more space //shift elements over within array; for( int i = this.size(); i >= insertIndex; --i ){ if( i == insertIndex ){ //at the 'created' space this.array[i] = 0; } else { //usual case this.array[i] = this.array[i-1]; } } } /********* Public Methods *********/ /** Accessors/Getters **/ /* Returns integer at the index * Throws IndexOutOfBoundsException if index is not w/i bounds [0...size) */ public int get( int index ){ // throw exception if invalid access; if( index < 0 ) throw new IndexOutOfBoundsException("index < 0 access: [" +index +"]"); else if( index >= this.size() ) throw new IndexOutOfBoundsException("index >= size() access [" +index +"]"); return this.array[ index ]; //return element at that index } /* Returns the number of elements in the list */ public int size(){ return this.numberElements; //return number of elements } /* Returns the full size of the containing array */ public int capacity(){ return this.array.length; } /* Returns true if there are 0 elements in the list */ public boolean isEmpty(){ return this.size() == 0; } /* Returns a copy of all the elements in the array; size = size() */ public int[] toArray(){ int[] copyArray = new int[ this.size() ]; //allocate an array with exactly the number of inserted elements //copy the array for( int i = 0; i < this.size(); ++i ){ copyArray[ i ] = this.get( i ); } return copyArray; //return the copy } /* Returns a String representation of the elements in the array; * (non-Javadoc) * @see java.lang.Object#toString() */ public String toString(){ return Arrays.toString( this.toArray() ); /* String ret = "[ "; //our returning string for( int i = 0; i < this.size(); ++i ){ ret += this.get( i ) + ( (i == this.size()-1)?"":", " ); } ret += " ]"; return ret; */ } /** Modifiers/Setters **/ /* Insert integer into array at index * If index is within existing array, [0-size), performs left shift then insert * If index is == size, inserts only * if index is > size throws */ public void insert(int index, int integer ){ this.checkSize(); if( index < 0 ) throw new IndexOutOfBoundsException("index < 0 access: [" +index +"]"); else if( index > this.size() ) throw new IndexOutOfBoundsException("index > size() access [" +index +"]"); if( index < this.size() ) this.insertShift( index ); //if need be shift elements this.array[index] = integer; //insert into array this.numberElements += 1; //increase number of elements } /* Appends integer onto the list * Returns the index integer was inserted to */ public int add( int integer ){ this.insert( this.size(), integer ); // perfomr insert at the size()'th index return this.size()-1; //return the previous size ( the insert index ) } /* Appends list of integers onto the list * Returns the last index inserted into */ public int add( int[] listOfInteger ){ for( int i : listOfInteger ){ this.add( i ); } return this.size()-1; } /* Removes element at index from the array * Returns the element removed * Throws IndexOutOfBoundsException if index is not w/i bounds [0...size) */ public int remove( int index ){ if( index >= this.size() || index < 0 ) throw new IndexOutOfBoundsException(index + " is outside of bounds 0-" + this.size() ); int temp = this.get( index ); //get value this.removeShift( index ); //removes the value of the index by shifting others over. takes care of this.numberElements -= 1; //decrease number of elements return temp; //return the element removed } /* Sets value of element at index * Returns the previous value of the element at index * Throws IndexOutOfBoundsException if index is not w/i bounds [0...size) */ public int set( int index, int integer ){ if( (index >= this.size() || index < 0) && this.size() != 0 ) throw new IndexOutOfBoundsException(index + " is outside of bounds 0-" + this.size() ); int temp = this.get( index ); // Get old value this.array[ index ] = integer; // Set new value return temp; // return old value } }