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);
            }
    
        }
    }
  • 相关阅读:
    FlatBuffers要点
    tarjan+缩点+强连通定理
    编程之美2.16 最长递增子序列
    Android Studio之多个Activity的滑动切换(二)
    Effective java读书札记第一条之 考虑用静态工厂方法取代构造器
    【PM】关于系统数据库和服务现场升级的一些看法
    用户及权限基础 2---- 权限
    Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效
    【转贴】gdb中的信号(signal)相关调试技巧
    基于新浪sae使用php生成图片发布图文微博
  • 原文地址:https://www.cnblogs.com/lfdestiny/p/9572743.html
Copyright © 2011-2022 走看看