/** * NOT SORTED * @author weiss */ class IntArrayList { public IntArrayList( ) { items = new int[ 0 ]; } public void add( int x ) { if( theSize == items.length ) ensureCapacity( theSize * 2 + 1 ); items[ theSize++ ] = x; } public void ensureCapacity( int cap ) { if( cap < theSize ) throw new IllegalArgumentException( ); int [ ] old = items; items = new int[ cap ]; for( int i = 0; i < theSize; i++ ) items[ i ] = old[ i ]; } /** * @param x the item to remove (NOT THE INDEX) * @return whether the remove was successful */ public boolean remove( int x ) { for( int p = 0; p < theSize; p++ ) if( items[ p ] == x ) { items[ p ] = items[ --theSize ]; return true; } return false; } public boolean contains( int x ) { for( int p = 0; p < theSize; p++ ) if( items[ p ] == x ) return true; return false; } public int size( ) { return theSize; } public boolean isEmpty( ) { return size( ) == 0; } public String toString( ) { String result = "[ "; for( int i = 0; i < theSize; i++ ) result += items[ i ] + " "; result += "]"; return result; } private int [ ] items; private int theSize; } public class Day26 { public static void main( String [ ] args ) { IntArrayList arr = new IntArrayList( ); arr.add( 8 ); arr.add( 4 ); arr.add( 1 ); arr.add( 6 ); arr.add( 12 ); arr.add( 3 ); System.out.println( arr ); arr.remove( 12 ); System.out.println( arr ); System.out.println( arr.size( ) ); } }