zoukankan      html  css  js  c++  java
  • 自定义实现一个简单的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
  • 相关阅读:
    利用border-radius画椭圆
    关于使用svg构建六边形蜂巢列表的方式
    JavaScript拖拽效果的原理及实现
    逆战班-JS的形参与实参
    前端面试&笔试汇总
    less学习---less的混合(mixin)
    less学习---less的嵌套规则
    less学习----less变量
    vue-cli3实现将数据导出为Excel表
    js中apply和call方法浅析
  • 原文地址:https://www.cnblogs.com/mature1021/p/10483947.html
Copyright © 2011-2022 走看看