• 自定义ArrayList


    import java.util.Iterator;
    import java.util.NoSuchElementException;
    
    
    /**
     * @Auther: FAN
     * @Date: 2018/9/1 21:41
     * @Description: 自定义ArrayList
     **/
    public class MyArrayList<AnyType>{
    
        //默认数组大小
        private static final int DEFAULT_CAPACITY = 10;
    
        private int  theSize;
        private AnyType[] theItems;
    
        //初始化
        public MyArrayList(){
            doClear();
        }
    
        public void doClear() {
            theSize = 0;
            ensureCapacity(DEFAULT_CAPACITY);
        }
    
        /**
         *
         * @return 返回集合元素大小
         */
        public int size(){
            return theSize;
        }
    
        /**
         *
         * @return 返回集合元素是否为空
         */
        public boolean isEmpty(){
            return theSize == 0;
        }
    
        public void trimToSize(){
            ensureCapacity(size());
        }
    
        /**
         *
         * @param index 角标
         * @return 返回角标为index的元素
         */
        public AnyType get(int index){
    
            if (index < 0 || index > theSize){
                throw new ArrayIndexOutOfBoundsException();
            }
            return theItems[index];
    
        }
    
        /**
         *
         * @param index 需要修改的角标
         * @param newAnyType 需要修改的AnyType
         * @return 返回旧的AnyType
         */
        public AnyType set(int index,AnyType newAnyType){
            if (index<0||index>theSize){
                throw new ArrayIndexOutOfBoundsException();
            }
            AnyType old = theItems[index];
            theItems[index] = newAnyType;
            return old;
        }
    
    
        private void ensureCapacity(int newCapatity) {
    
            if (newCapatity < theSize){
                return;
            }
    
            AnyType[] old = theItems;
            theItems = (AnyType[]) new Object[newCapatity];
            for (int i = 0;i<size();i++){
                theItems[i] = old[i];
            }
        }
    
    
        /**
         *
         * @param anyType 待添加的元素
         * @return 如果anyType添加成功返回true
         */
        public boolean add(AnyType anyType){
            add(theSize,anyType);
            return true;
        }
    
        public void add(int index, AnyType anyType) {
    
            if (theItems.length == theSize){
                ensureCapacity(theSize*2+1);
            }
            for (int i = theSize;i>index;i--){
                theItems[i] = theItems[i-1];
            }
            theItems[index] = anyType;
            theSize++;
    
        }
    
        public AnyType remove(int index){
            if (index<0||index>theSize){
                throw new ArrayIndexOutOfBoundsException();
            }
            AnyType removeItem = theItems[index];
            for (int i = index;i<theSize-1;i++){
                theItems[i] = theItems[i+1];
            }
            theSize--;
            return removeItem;
        }
    
        public Iterator<AnyType> iterator(){
            return new ArrayListIterator();
        }
    
        private class ArrayListIterator implements Iterator<AnyType>{
    
            private int current = 0;
    
            public boolean hasNext() {
                return current<size();
            }
    
            public AnyType next() {
                if (!hasNext()){
                    throw new NoSuchElementException();
                }
                return theItems[current++];
            }
    
            public void remove() {
                MyArrayList.this.remove(--current);
            }
    
        }
    }
  • 相关阅读:
    C#中使用My实现单例应用程序
    喝着啤酒学Python(2):第一个HelloWorld
    再读《精通css》04:盒模型和空白边叠加
    再读《精通css》07:圆角
    再读《精通css》08:阴影
    @ResponseBody 乱码
    再读《精通css》05:定位、浮动与清理
    关于javascript面向对象的一点思考
    再读《精通css》06:背景图片
    【求解释】关于第三方接口调用中安全的疑问
  • 原文地址:https://www.cnblogs.com/lfdestiny/p/9572743.html
Copyright © 2020-2023  润新知