Java集合之自己实现ArrayList

版权声明:本文为博主原创文章,转载请声明出处并添加原文链接。 https://blog.csdn.net/azsx02/article/details/83062350
package com.huhu.collection;

public class MyArrayList<E> {

    private Object[] elementData;

    private int size;

    public int getSize() {
        return size;
    }

    public boolean isEmtpy() {
        return size == 0;
    }

    public MyArrayList() {
        this(10);
    }

    public MyArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else {
            // throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public boolean add(E obj) {
        // 扩容
        ensureCapacity();

        elementData[size++] = obj;
        return true;
    }

    public E get(int index) {
        rangeCheck(index);

        return (E) elementData[index];
    }

    public E remove(int index) {
        rangeCheck(index);

        E oldValue = (E) elementData[index];
        System.arraycopy(elementData, index+1, elementData, index, size-index-1);
        elementData[--size]=null;
        return oldValue;
    }

    public boolean remove(E o) {
        if (o == null) {
            for (int index=0; index < size; index++) {
                if (elementData[index] == null) {
                    remove(index);
                    return true;
                }
            }
        } else {
            for (int index = 0; index < size; index++)
                if (o == elementData[index]) {  // o.equals(elementData[index])
                    remove(index);
                    return true;
                }
        }

        return false;
    }

    public E set(int index, E element) {
        rangeCheck(index);

        E oldValue = (E) elementData[index];
        elementData[index] = element;
        return oldValue;
    }

    public void add(int index, E element) {
        ensureCapacity();
        rangeCheck(index);

        System.arraycopy(elementData, index, elementData, index+1, size-index);
        elementData[index] = element;
        size++;
    }

    private void rangeCheck(int index) {
        if (index <0 || index >= size) {
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void ensureCapacity() {
        if (size == elementData.length) {
            Object[] newElementData = new Object[size*2];
            System.arraycopy(elementData, 0, newElementData, 0, elementData.length);
            elementData = newElementData;
        }
    }

    public static void main(String[] args) {
        MyArrayList<String> myArrayList = new MyArrayList<String>(3);
        myArrayList.add("111");
        myArrayList.add("22");
        myArrayList.add("33");
        myArrayList.add("ss");
        myArrayList.add("55");
        myArrayList.add(8);
        System.out.println(myArrayList.getSize());
        System.out.println(myArrayList.get(3));
        System.out.println(myArrayList.isEmtpy());
        myArrayList.remove(3);
        System.out.println(myArrayList.get(3));

        // 测试remove   equals和==

        System.out.println("测试remove   equals和==");
        myArrayList.add("mmm");
        myArrayList.add(new String("nnnn"));
        System.out.println(myArrayList.getSize());
        myArrayList.remove("nnnn");
        System.out.println(myArrayList.getSize());
        
    }
}

猜你喜欢

转载自blog.csdn.net/azsx02/article/details/83062350