【笔记】Java ArrayList

2021/8/22 9:36:06

本文主要是介绍【笔记】Java ArrayList,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

存储结构

  • transient Object[] elementdata
  • DEFAULT_CAPACITY =10

初始化

  • 默认构造器初始化

  • 带参构造器

    • int initcapacity

    • ArrayList(Collection<? extends E> c)

主要操作

  • 添加数据

    • add(E e)
    • add(int index, E e)
    • addAll(Collection<? extends E> c)
    • addAll(int index, Collection<? extends E> c)
    • index 不能大于 size
  • 删除数据

    • remove(int index)
    • remove(Obejct obj)
    • removeAll(Collection<? extends E> c), 删除list中与集合C 中相同的元素
    • removeIf(Predicate<? super E> filter) ,通过filter 删掉满足条件的元素
    • clear()
  • 查询数据

    • get(int index) ,根据索引进行查询
    • contains(Object obj) ,查询是否存在相关对象
    • indexOf(Objext o)
  • 修改数据

    • set(int index, Object obj),
    • replaceAll(UnaryOperator operator)
  • 排序

    • sort(Comparator<? extends super E> c)
  • 扩容

    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
    


这篇关于【笔记】Java ArrayList的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程