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
  • 相关阅读:
    解决input 输入框频繁请求问题,如果拿取最后一次接口返回的值
    记录两个小问题
    axios 如何取消请求
    给vue组件绑定原生事件
    Vue3 与 Vue2的不同之处一 简单介绍 Vue 核心最基本的功能
    js将数组对象中,以某个值相同的对象合并成一个;即把某个值相同的对象内容合并成一个
    postcss-preset-env
    webpack5 tree shaking
    深拷贝
    webpack 性能优化
  • 原文地址:https://www.cnblogs.com/mature1021/p/10483947.html
Copyright © 2011-2022 走看看