美文网首页
动态数组--java

动态数组--java

作者: 不死鸟F21 | 来源:发表于2022-01-17 15:47 被阅读0次
    public class AnyArray<E> {
    
        private E[]data;
        private int size;
        //构造函数
        public AnyArray(int cap){
            data = (E[]) new Object[cap];
            size = 0;
        }
        public AnyArray(){
            this(10);
        }
        public int getCap(){
            return data.length;
        }
        public int getSize(){
            return size;
        }
        public boolean inEmpty(){
            return size ==0;
        }
        public void addLast(E e){
    //        if (size==data.length)
    //            throw  new IllegalArgumentException("full....");
    //        data[size] = e;
    //        size++;
            add(size,e);
        }
        public void addFirst(E e){
            add(0,e);
        }
    
        public void  add(int pos,E e){
    
            if (pos<0|| pos>size)
                throw  new IllegalArgumentException("add failed, pos Error.");
    
            if (size==data.length)
                resize(data.length*2);
    
            for (int index=size-1;index>=pos;index--){
                data[index+1] =data[index]; //后移
            }
            data[pos] = e;
            size++;
    
        }
        public void modify(int pos,E e){
            if (pos<0 || pos>size)
                throw new IllegalArgumentException("modfiy failed, Pos Error");
            data[pos] = e ;
        }
        public E get(int pos){
            if (pos<0 || pos>size)
                throw new IllegalArgumentException("get failed, Pos Error");
            return data[pos];
        }
        public void set(int pos, E e){
            if (pos<0 || pos>size)
                throw new IllegalArgumentException("set failed, Pos Error");
            data[pos] = e;
        }
        public int find(E e){
            for (int i=0;i<size;i++){
                if (data[i] ==e)
                    return i;
            }
            return -1;
        }
        public E  remove(int pos){
            if (pos<0 || pos>=size)
                throw new IllegalArgumentException("remove failed, Pos Error");
            E pos_data = data[pos];
    
            for (int i=pos+1;i<size;i++){
                data[i-1]=data[i];
            }
            size--;
            if (size == data.length /4 && data.length/2 !=0){
                System.out.println("-- resize.....");
                resize(data.length/2);
            }
            return pos_data;
        }
        public E removeFirst(){
            return remove(0);
        }
        public void removeElement(E e){
            int index = find(e);
            if (index !=-1){
                remove(index);
            }
        }
        public E removeLast(){
            return remove(size-1);
        }
        public void print(){
            for (E e :data){
                System.out.println(e);
            }
        }
        public String toString(){
            StringBuilder res = new StringBuilder();
            res.append(String.format("Array size is %s, Cap is %s\n",size,getCap()));
            res.append("[");
            for (int i=0; i<size ; i++){
                res.append(data[i]);
                if (i!=size-1)
                    res.append(", ");
            }
            res.append("]");
            return res.toString();
    
        }
    
        private void resize(int newCap){
            if (newCap ==0)
                return;
            System.out.println("resize......");
            E[] newData = (E[]) new Object[newCap];
            for (int i=0;i<size;i++)
                newData[i] = data[i];
            data = newData;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:动态数组--java

          本文链接:https://www.haomeiwen.com/subject/iuvchrtx.html