Java - - ArrayList 源码解析
程序员文章站
2022-10-03 17:12:02
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学习随笔:权限修饰符以及特殊抽象类(接口)
下一篇: 创建型模式:原型模式
推荐阅读
-
基于Spring注解的上下文初始化过程源码解析(一)
-
java项目源码哪里找(java初学者练手项目)
-
axios 源码解析(下) 拦截器的详解
-
Spring源码解析之ConfigurableApplicationContext
-
【Java并发编程】24、Synchronized实现原理解析
-
angularjs 源码解析之injector
-
angularjs 源码解析之scope
-
netty源码解析(4.0)-28 ByteBuf内存池:PooledByteBufAllocator-把一切组装起来
-
SpringBoot 源码解析 (七)----- Spring Boot的核心能力 - SpringBoot如何实现SpringMvc的?
-
SpringBoot 源码解析 (六)----- Spring Boot的核心能力 - 内置Servlet容器源码分析(Tomcat)