自定义实现一个简单的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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值