Java - - ArrayList 源码解析
程序员文章站
2022-05-04 08:55:41
ArrayListArrayList是一个数组队列,相当于动态数组。与Java中的数组相比,它的容量能动态增长。继承父类 : AbstractList实现的接口有 List, RandomAcces, Cloneable, java.io.Serializablepublic class ArrayList extends AbstractList implements List, Rand...
ArrayList
ArrayList是一个数组队列,相当于动态数组。与Java中的数组相比,它的容量能动态增长。
- 继承父类 : AbstractList<E>
- 实现的接口有 List<E>, RandomAcces, Cloneable, java.io.Serializable
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first element is added.
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
// . . .
}
可以看出ArrayList中用于存放对象的数组为Object[] elementData;
其默认长度(存放数据且长度不超过10)为DEFAULT_CAPACITY = 10;
,而Size
表示实际存放数据的大小。
构造方法
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
默认情况下,没有传参数,数组长度为零。参数可以是数组长度,也可以是另一个collection集合。
、、、还没解完,有空再看
本文地址:https://blog.csdn.net/sabot_v/article/details/107348215
推荐阅读
-
死磕 java同步系列之Phaser源码解析
-
Feign源码解析系列-那些注解们
-
imutils库源码解析,看他如何调用opencv基本函数
-
Java程序员必备基础:内部类解析
-
Java接口幂等性设计原理解析
-
JAVA中4种解析XML文件的方法
-
Java静态泛型使用方法实例解析
-
Mybaits 源码解析 (十)----- 全网最详细,没有之一:Spring-Mybatis框架使用与源码解析
-
Mybaits 源码解析 (八)----- 全网最详细,没有之一:结果集 ResultSet 自动映射成实体类对象(上篇)
-
Mybaits 源码解析 (九)----- 全网最详细,没有之一:一级缓存和二级缓存源码分析