java ArrayList 的简单实现

参照 数据结构与算法分析 (java语言描述47-48页) 新年快乐!我不太聪明的亚子 找不到工作…

import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyArrayList<AnyType> implements Iterable<AnyType> {
    /*
    * list 的初始容量 DEFAULT_CAPACITY
    * list的有效个数 theSize
    * 数据域  theItems
    * 操作
    * 1.创建MyArrayList()  清空clear()
    * 2.返回元素个数size()   判断是否为空isEmpty
    * 3.获取指定索引的值get()   修改指定位置的值set()
    * 4.确保容量够用 扩容方法ensureCapacity()
    * 5.末尾插入 和指定位置插入  add()  利用函数重载实现
    * 6.删除指定索引的元素 remove
    * 7.迭代的实现  通过实现iterator接口 实现MyArrayList的迭代  并交给iterator()方法 让它返回一个可迭代对象
    * */
    private static final  int DEFAULT_CAPACITY=10;
    private int theSize;
    private AnyType []theItems;

    //1创建 初始化
    public MyArrayList(){
        theSize=0;
        //theItems = (AnyType []) new Object[DEFAULT_CAPACITY];//这句已经在ensureCapacity实现,所以
        ensureCapacity(DEFAULT_CAPACITY);
    }

    //清空
    public void clear(){
        theSize = 0;
        //theItems = (AnyType []) new Object[DEFAULT_CAPACITY];//因为清空前的容量可能大于10  所以它也需要重置
        ensureCapacity(DEFAULT_CAPACITY);
    }

    //2.返回元素个数
    public int size(){
        return theSize;
    }

    //判断是否为空
    public  boolean isEmpty(){
        return  size()==0;
    }

    //3.获取指定索引的值get()
    public  AnyType get(int idx){
        if(idx < 0 || idx>=size()){
            throw new ArrayIndexOutOfBoundsException();
        }
        return theItems[idx];
    }

    //修改指定位置的值set() 并返回修改前的值
    public  AnyType set(int idx,AnyType newVal){
        if(idx < 0 || idx>=size()){
            throw new ArrayIndexOutOfBoundsException();
        }
        AnyType old = theItems[idx];//获取修改前的值
        theItems[idx] = newVal;//修改
        return old;
    }
    //4.确保容量够用方法
    private void ensureCapacity(int newCapacity){
        //采用把原来数组的值 赋给新数组的值
        if(newCapacity < size()){return;}

        AnyType [] old= theItems; //原来的数组
        theItems = (AnyType []) new Object[newCapacity];//新数组
        for (int i = 0; i <size() ; i++) {
            theItems[i] = old[i];
        }
    }

    //5.指定位置插入
    public void add(int idx,AnyType val){
        //判断 idx 是否合法
        if(idx < 0 || idx >size()){
            throw new ArrayIndexOutOfBoundsException();
        }
        //判断容量是否满
        if (theItems.length == size()){
            //扩容
            ensureCapacity(size()*2+1);
        }
        // 指定位置插入
        //它后面的元素后移
        for (int i = size();i>idx;i--){
            theItems[i]=theItems[i-1];
        }

        theItems[idx] = val;//这个位置的值已经空出来了 给它赋值
        theSize++;
    }

    //尾插
    public boolean add(AnyType val){
        add(size(),val);
        return true;
    }

    //6.指定位置删除 并返回删除的值
    public AnyType remove(int idx){
        //判断 idx 是否合法
        if(idx < 0 || idx >=size()){
            throw new ArrayIndexOutOfBoundsException();
        }
        AnyType removeItem = theItems[idx];//要删除的值
        //它后面的元素前移
        for (int i = idx; i <size()-1 ; i++) {
            theItems[i]=theItems[i+1];
        }
        theSize--;

        //减容操作
        if (size()==theItems.length/2){
            ensureCapacity(theItems.length/2);
        }
        return  removeItem;
    }


    @Override
    public Iterator<AnyType> iterator() {
        /*
        * 实现 接口里Iterable的iterator的方法  返回一个可迭代对象
        * */
        return new ArrayListIterator(){
        };
    }

    //7.迭代的实现
    private class ArrayListIterator implements java.util.Iterator<AnyType>{
        /*实现Iterator接口的 hasNext 和 next方法*/
        private int current = 0;
        @Override
        public boolean hasNext() {
            return current<size();
        }

        @Override
        public AnyType next() {
            if (!hasNext()){
                throw new NoSuchElementException();
            }
            return theItems[current++];//先取值后自增
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值