java实现Arraylist

package myDataStructures;
//import javax.swing.text.html.HTMLDocument;
        import java.util.Arrays;
        import java.util.Iterator;
        import java.util.Random;
//import java.util.TreeMap;

public class MyArrayList<T> implements Iterable<T> {

    private int theSize;
    private T[] theArray;
    private final int initialLength=10;

    public MyArrayList(){
        doClear();
    }

    public MyArrayList(int capacity){
        doClear(capacity);
    }

    public boolean clear(){
        doClear();
        return theSize==0;
    }

//    public

    private void doClear(){
        this.theSize=0;
        extendCapacity(this.initialLength);
    }

    private void doClear(int capacity){
        this.theSize=0;
        extendCapacity(capacity);
    }

    private void extendCapacity(int capacity){
        if (capacity< theSize){
            return;
        }

        else {
            T[] theOld=theArray;
//            @SuppressWarnings("Unchecked")
            theArray=(T[]) new Object[capacity]; 
            for (int i=0; i<theSize; i++){
                theArray[i]=theOld[i]; 
            }
        }
    }

    public int getTheSize(){
        return theSize;
    }

    public boolean isEmpty(){
        return theSize== 0;
    }

    public T get(int index){
        if (index<0 || index> theSize){
            throw new ArrayIndexOutOfBoundsException(index); //抛出异常
        }
        else {
            return theArray[index];
        }
    }

    public reTurnDouble<T,Boolean> set(T input){
        return set(theSize-1,input);
    }

    public reTurnDouble<T,Boolean> set(int index,T input){ 
        if (index<0 || index>=theSize){
            throw new ArrayIndexOutOfBoundsException(index);
        }
        else {
            T old=theArray[index]; 
            theArray[index]=input;// set
            return new reTurnDouble<>(old, true);
        }
    }

    public void add(T input){ 
        add(theSize,input);
    }

    public void add(int index,T input){
        if (index<0 || index>theSize){
            throw new ArrayIndexOutOfBoundsException(index);
        }
        else {
            if (theArray.length==theSize){
                extendCapacity(2*theSize+1); 
            }
            for (int i=index;i<theSize;i++){
                theArray[i+1]=theArray[i]; 
            }
            theArray[index]=input;
            ++theSize;
        }
    }

    public void remove(){ 
        remove(theSize-1);
    }

    public void remove(int index){ 
        if (index<0 || index>=theSize){
            throw new ArrayIndexOutOfBoundsException(index);
        }
        else {
            if (theSize<=(int)((theArray.length-1)/2)){
                extendCapacity((int)((theArray.length-1)/2));
            }
            for (int i=index;i<theSize-1;i++) {
                theArray[i] = theArray[i + 1]; 
            }
        }
    }

    public String myToString(){
        T[] return1=(T[]) new Object[theSize];
        for (int i=0;i<theSize;i++){
            return1[i]=theArray[i];
        }
        return Arrays.toString(return1);
    }

    @Override
    public Iterator<T> iterator(){
        return new Xiangqian();
    }

    class Xiangqian implements Iterator<T>{ 
        private int num;
        private Xiangqian(){
            num=-1;
        }
        public Xiangqian(int startIndex){
            num=startIndex;
        }
        @Override
        public boolean hasNext(){
            if (num<theSize-1){
                return true;
            }
            else {
                return false;
            }
        }

        @Override
        public T next(){
            if (hasNext()){
                return theArray[++num];
            }
            throw new ArrayIndexOutOfBoundsException();
        }

        @Override
        public void remove() {
            System.out.println(next());
            MyArrayList.this.remove(num--);
            
        }
    }

    class reTurnDouble<A,B> {
        A a;
        B b;
        protected reTurnDouble(A aInput,B bInput){
            this.a=aInput;
            this.b=bInput;
        }
    }
}

class testMyArrayList{
    public static void main(String[] args){
        MyArrayList<Integer> list=new MyArrayList<>(10);
        Random random=new Random();
        for (int i=0;i<10;i++){
            try {
                list.add(random.nextInt(100)-50);
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                System.out.printf(String.format("%d is over\n",i));
            }
        }
        System.out.println(list.myToString());
        for (int i=0;i<list.getTheSize();i++){
            System.out.println(list.get(i));
        }
        for (Integer x:list){ //引用了Iterable 所以可以用foreach了
            System.out.println(x);
        }
        Iterator<Integer> ite=list.iterator(); 
        while (ite.hasNext()){ 
            System.out.printf("now is %s,num is %d\n",String.valueOf(ite.hasNext()),ite.next());
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值