自定义实现一个简单的List列表

package futrue;

import java.util.concurrent.locks.ReentrantLock;

public class CustomArrayList<E> {
    private ReentrantLock reentrantLock = new ReentrantLock(true);
    private Object[] objects = null;
    private int size = 0;
    private int length = 10;

    public CustomArrayList() {
        objects = new Object[length];
    }
    // 添加
    public void add(E e) {
        reentrantLock.lock();
        try {
            capacity();
            objects[size] = e;
            size++;
        } catch (Exception e2) {
            // TODO: handle exception
        } finally {
            reentrantLock.unlock();
        }
    }
    // 获取
    @SuppressWarnings("unchecked")
    public E get(int index) {
        reentrantLock.lock();
        try {
            checkCrossingTheLine(index);
            return (E) objects[index];
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            reentrantLock.unlock();
        }
        return null;

    }

    // 删除
    public void rmove(int index) {
        reentrantLock.lock();
        try {
            checkCrossingTheLine(index);
            if (index == size - 1) {// 如果是最后一个
                objects[index] = null;
                size--;
            } else {// 非最后一个移除
                for (int i = index; i < size - 1; i++) {
                    objects[i] = objects[i + 1];
                }
                objects[size - 1] = null;
                size--;
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            reentrantLock.unlock();
        }

    }

    // 大小
    public int size() {
        return size;
    }

    // 清除
    public void clear() {
        reentrantLock.lock();
        try {
            for (int i = 0; i < size; i++) {
                objects[i] = null;
            }
            size = 0;
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            reentrantLock.unlock();
        }
    }

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

    // 扩容
    public void capacity() {
        if (size >= length) {
            Object[] newArry = new Object[(int) 2 * length];
            for (int i = 0; i < objects.length; i++) {
                newArry[i] = objects[i];// 将旧数组的元素逐个复制到新的数组里,
            }
            objects = newArry;// 替换旧数组
            newArry = null;
        }
    }

    // 检查index是否越界
    private void checkCrossingTheLine(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size);
        }
    }
}






测试
package futrue;

public class CustomArrayListTest {
public static void main(String[] args) {
    CustomArrayList<String>customArrayList=new CustomArrayList<>();
    System.out.println(":初始大小"+customArrayList.size());
    customArrayList.add("mature");
    System.out.println("size:"+customArrayList.size());
    customArrayList.add("sound");
    System.out.println("size:"+customArrayList.size());
    System.out.println(customArrayList.get(0));
    System.out.println(customArrayList.get(1));
    customArrayList.rmove(0);
    System.out.println("size:"+customArrayList.size());
    System.out.println(customArrayList.get(0));
    
}
}


:初始大小0
size:1
size:2
mature
sound
size:1
sound
原文地址:https://www.cnblogs.com/mature1021/p/10483947.html