手撸ArrayList

时间:2025-03-06 19:24:57
//把老的数组数据copy给新数组
         (elementData, 0, newArray, 0, );
                elementData = newArray;
            }
            elementData[size++] = e;
        }
        
        public MyArrayList() {
            this(10);
        }
    
        public MyArrayList(int initSize) {
            if(initSize<0){
                try {
                    throw new IllegalAccessException();
                } catch (IllegalAccessException e) {
                    ();
                }
            }
            elementData =  new Object[initSize];
        }
        
        public E get(int index){
            return (E) elementData[index] ;
        }
        
        public boolean remove(int index){
            if(index>size || index<0){
                return false;
            }
            for (int i = index; i < size; i++) {
                elementData[index] = elementData[index+1] ;
            }
            return true ;
        } 
        
        public static void main(String[] args) {
            
            
            MyArrayList<String> list  = new MyArrayList<String>();
            ("aa");
            ("bb");
            ("cc");
            ("dd");
            ("ee");
            ("ff");
            (2);
            for (int i = 0; i < (); i++) {
                ((i));
            }
            
        }
        
        
    }