ArrayList源码简单理解
来源:互联网 发布:知乎网站 源码 编辑:程序博客网 时间:2024/06/05 00:21
成员变量:
Object数组 elementData。
size 表示当前大小。
构造函数:
用户传入initialCapacity为初始的大小为elementData的大小,如果没有传则大小默认为10。
modcount:
函数中多次使用到了modcount,这个modcount是在使用迭代器遍历的时候,用来检查列表中的元素是否发生结构性变化就是说元素有没有改变,主要在多线程环境下需要使用,防止一个线程正在迭代遍历,另一个线程修改了这个列表的结构。ArrayList是非线程安全的。
其他成员方法:
1.public void trimToSize();
缩容函数。
modcount++;
如果size小于数组长度,用Arrays.copyOf()拷贝大小为size
2.public void ensureCapacity(int minCapacity);
检查空间是否足够,minCapacity是判断的依据;
modcount++;
如果minCapacity > 数组长度,扩大1.5倍;用Arrays.copyOf()拷贝;
3.public int size()
返回当前大小;
4. public boolean isEmpty() ;
判断该容器是否为空;
size == 0;
5.public boolean contains(Object o)
检查是否包含某一个元素
6.public int lastIndexOf(Object o);
从0下标开始检查元素,返回该元素的下标
7.public Object clone() ;
克隆方法,深拷贝,
8.public Object[] toArray() ;public <T> T[] toArray(T[] a)
将容器内的元素拷贝到数组中;Object数组实现和泛型实现
9.public E get(int index);
获取index号位置元素的值
10.public E set(int index, E element);
将index号位置元素的值改为element
11.public boolean add(E e)
在尾部添加一个元素,先调用ensureCapacity(size+1),再添加元素
12.public void add(int index, E element)
在index号位置插入element;使用System.arraycopy(elementData, index, elementData, index + 1,
size - index)向后拷贝之后将element覆盖到index位置
13. public E remove(int index) ;
删除index位置的元素,返回删除元素的值
modCount++;
14.public boolean remove(Object o);
删除某一元素,调用fastRemove(int index)方法;
15.private void fastRemove(int index)
删除元素;
umMoved = size - index - 1;
System.arraycopy(elementData, index+1, elementData, index, numMoved);
16.public void clear() ;
清空容器,每个位置都赋为null;size = 0;
17.public boolean addAll(Collection<? extends E> c);
将另一个容器中的全部元素添加到该容器的末尾
18.public boolean addAll(int index, Collection<? extends E> c);
将另一个容器中的全部元素从index位置全部添加到该容器
19.protected void removeRange(int fromIndex, int toIndex);
modCount++
从fromIndex位置到toIndex位置的元素全部删除
- ArrayList源码简单理解
- java--ArrayList源码理解
- ArrayList源码理解
- ArrayList源码简单剖析
- 从源码理解ArrayList.java
- ArrayList源码解析及简单自定义ArrayList
- ArrayList简单的源码分析
- ArrayList源码的阅读与理解
- ArrayList的源码简单分析分析
- 从源码来理解ArrayList和LinkedList区别
- 通过jdk源码理解ArrayList和LinkedList区别
- 深入理解JAVA集合系列四:ArrayList源码解读
- java中ArrayList源码方法理解--add(),remove()
- ArrayList源码
- ArrayList源码
- ArrayList源码
- ArrayList源码
- ArrayList源码
- spring 事物的一些理解 推荐一个我认为Spring事物写得很好的文章。 文章链接:http://www.codeceo.com/article/spring-transaction
- flume01
- (集合【Map、可变参数、Collections】)
- Postgres error:You might need to increase max_locks_per_transaction
- Spring的Task定时任务
- ArrayList源码简单理解
- MYSQL命令创建并选择数据库
- 关于Arduino Mega2560的最基本介绍
- maven java resource 报错
- spring 事物的一些理解
- 2671 3-2 构造函数的调用
- nginx http2 源码分析
- Unity 变换物体位置的几种方法
- Codevs 2455 繁忙的都市