簡單實現ArrayList



/**簡單實現的數組列表
 * 
 * */


public class MyArrayList
{
      //存儲數組
      private Object [] elementData;
      private int size;


      public MyArrayList ( int initCapacity )
      {
            if ( initCapacity < 0 )
                  throw new RuntimeException("初始化長度必須大於0");
            elementData = new Object [initCapacity];
      }


      public MyArrayList ()
      {
            this(10);
      }


      public boolean isEmpty ()
      {
            return size == 0;
      }


      public int size ()
      {
            return size;
      }


      //添加元素
      public void add ( Object obj,int index )
      {
            //是否擴容
            ensureCapacity(size + 1);
            RangeCheck(index) ;
            
            int numMoved = size - index ;//要移動的個數


            if ( numMoved > 0 )
                  System.arraycopy(elementData, index , elementData, index+1, numMoved);
            
            elementData [index] = obj; 
            size++;
            
      }
      
      public void add ( Object obj )
      {
            ensureCapacity(size + 1);
            elementData [size++] = obj;
      }


      public Object get ( int index )
      {
            RangeCheck(index);
            return elementData [index];
      }


      public Object remove ( int index )
      {
            RangeCheck(index);


            Object oldObj = elementData [index];


            int numMoved = size - index - 1;//要移動的個數


            if ( numMoved > 0 )
                  System.arraycopy(elementData, index + 1, elementData, index, numMoved);
            elementData [--size] = null;
            return oldObj;
      }


      public Object remove ( Object obj )
      {
            for ( int i = 0; i < size; i++ )
            {
                  if ( elementData [i].equals(obj) )
                  {
                        return remove(i);
                  }
            }
            return null;
      }


      private void RangeCheck ( int index )
      {
            if ( index >= size )
            {
                  throw new RuntimeException("訪問數據小標越界");
            }


      }


      private void ensureCapacity ( int minCapacity )
      {
            int oldCapacity = elementData.length;
            if ( minCapacity > oldCapacity )
            {
                  int newCapacity = ( oldCapacity * 3 ) / 2 + 1;
                  if ( newCapacity < minCapacity )
                        newCapacity = minCapacity;
                  //  elementData = Arrays.copyOf(elementData, newCapacity);
                  Object [] newElements = new Object [newCapacity];
                  System.arraycopy(elementData, 0, newElements, 0, oldCapacity);
                  elementData = newElements;
            }


      }


      public static void main ( String [] args )
      {
            //    ArrayList list=new ArrayList();
            //  System.out.println(list.get(-1));


            MyArrayList list = new MyArrayList(2);
            list.add("111");
            list.add("222");
            list.add("333");
            list.add("444");
          //  list.remove("2222");
            
            list.add("12222", 1);
            System.out.println(list.size);
            for ( int i = 0; i < list.size; i++ )
            {
                  System.out.println(list.get(i));
            }
      }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章