Java中的ArrayList解析

Java里面的ArrayList解析

ArrayList

继承了AbstractList,RandomAccess,Cloneable,Serializable。底层是一个Object数组。

构造方法

  • public ArrayList(int capacity)此时的ArrayList大小为capacity
  • public ArrayList()此时的public ArrayList()大小为10
  • public ArrayList(Collection<? extends E> c)此时的大小为c.size()

方法

  • public void trimToSize()使得ArrayList的大小等于本身的size
  • public void ensureCapacity(int minCapacity) 此时数组的长度 Math.max(current * 1.5, minCapacity)
  • public int size() 数组里面元素的个数
  • public boolean isEmpty() 数组是否为空
  • public boolean contains(Object e) 判断数组里是否包含此元素
  • public int indexOf(Object e) 顺序循环数组找到e的索引
  • public int lastIndexOf(Object e) 逆序循环数组找到e的索引
  • public Object clone() 数组的shallow copy(元素没有被cloned), 指向同一个元素
  • 如果是deepcopy,指向不同的元素,两个数组完全独立
  • public Object[] toArray() 生成一个新的array,这个array是独立于ArrayList
  • public T[] toArray(T[] a)
  • public E get(int index) 返回index对应的元素
  • public E set(int index, E e) 返回index开始对应的元素
  • public boolean add(E e) 数组的末尾增加一个元素
  • public void add(int index, E e) 指定位置插入e,index及后面元素通过复制,向后移动一位
  • public E remove(int index) 删除index对应的元素,index之后的元素通过复制,向前移动一位
  • public void clear() 通过Arrays.fill(data, 0, size, null)清除数组
  • public boolean addAll(Collection<? extends E> c) 调用addAll(size, c)
  • public boolean addAll(int index, Collection<? extends E> c) 首先判断数组是否需要扩容,然后复制原先数组到新的数组,最后在新的数组里面插入c
  • protected void removeRange(int fromIndex, int toIndex) 复制新的数组,去除掉range之间的部分 System.arraycopy(data, toIndex, data, fromIndex, size - toIndex)
  • private void checkBoundInclusive(int index) 检测是否在 if (index > size)
  • private void checkBoundExclusive(int index) 检测是否在 if (index >= size)
  • boolean removeAllInternal(Collection<?> c) 循环遍历删除数组里面含有的c
  • boolean retainAllInternal(Collection<?> c) 留下两个数组的公共元素
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值