zoukankan      html  css  js  c++  java
  • 自定义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);
            }
    
        }
    }
  • 相关阅读:
    python爬取动态网页数据,详解
    几行代码轻松搞定python的sqlite3的存取
    14、Iterator跟ListIterator的区别
    13、Java菜单条、菜单、菜单项
    12、借助Jacob实现Java打印报表(Excel、Word)
    11、借助POI实现Java生成并打印excel报表(2)
    10、借助POI实现Java生成并打印excel报表(1)
    9、JcomboBox下拉框事件监听
    8、单选按钮(JRadioButton)和复选框(JCheckBox)
    java swing 添加 jcheckbox复选框
  • 原文地址:https://www.cnblogs.com/lfdestiny/p/9572743.html
Copyright © 2011-2022 走看看