ArrayList 是一个动态数组,线程不安全 ,允许元素为null。

ArrayList的数据结构是数组,查询比较方便。

ArrayList类的接口

public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable{

RandomAccess:RandmoAccess是一个标记接口,用于被List相关类实现。他主要的作用表明这个相关类支持快速随机访问。在ArrayList中,我们即可以通过元素的序号快速获取元素对象——这就是快速随机访问。除了List的“快速随机访问”,还可以“通过Iterator迭代器访问”。
Cloneable:实现该接口的类可以对该类的实例进行克隆(按字段进行复制)。
Serializable:ArrayList支持序列化,能通过序列化去传输。

构造方法

ArrayList(),初始化的时候,先分配一个空数组。添加一个元素时,容量就会扩展到DEFAULT_CAPACITY,也就是10。

   /**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_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.
*
* 初始化的时候,分配一个空数组。添加一个元素时,容量就会扩展到DEFAULT_CAPACITY,也就是10。
* 关键字transient表示属性不会被序列化。
*/
transient Object[] elementData; // non-private to simplify nested class access /**
* Default initial capacity.
* 默认容量为10
*/
private static final int DEFAULT_CAPACITY = 10;

往ArrayList中添加数据的方法add() 如下:

  /**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
// 扩容
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}

扩容时,ensureCapacityInternal()方法内部调用的是grow()方法。

数组扩容。如果插入数据时容量不够,就将容量扩大为1.5倍。
扩容的过程就是数组拷贝 Arrays.copyOf的过程,每一次扩容就会开辟一块新的内存空间和数据的复制移动
 grow()方法 如下所示:

  /**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
// 左移一位表示原来的0.5倍,以下是将容量扩大为1.5倍
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);
}

get(int index)方法很简单,就是检查一下小心数组越界,然后根据下标返回数组元素

    /**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
rangeCheck(index); return elementData(index);
} @SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}

参考博客 :

https://www.jianshu.com/p/02f8696bf4cf

java集合: ArrayList源码浅析的更多相关文章

  1. Java集合ArrayList源码解读

    最近在回顾数据结构,想到JDK这样好的代码资源不利用有点可惜,这是第一篇,花了心思.篇幅有点长,希望想看的朋友认真看下去,提出宝贵的意见.  :) 内部原理 ArrayList 的3个字段 priva ...

  2. Java集合-ArrayList源码解析-JDK1.8

    ◆ ArrayList简介 ◆ ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAcc ...

  3. Java基础—ArrayList源码浅析

    注:以下源码均为JDK8的源码 一. 核心属性 基本属性如下: 核心的属性其实是红框中的两个: //从注释也容易看出,一个是集合元素,一个是集合长度(注意是逻辑长度,即元素的个数,而非数组长度) 其中 ...

  4. Java集合——ArrayList源码详解

    ) ArrayList 实现了RandomAccess, Cloneable, java.io.Serializable三个标记接口,表示它自身支持快速随机访问,克隆,序列化. public clas ...

  5. Java集合-ArrayList源码分析

    目录 1.结构特性 2.构造函数 3.成员变量 4.常用的成员方法 5.底层数组扩容原理 6.序列化原理 7.集合元素排序 8.迭代器的实现 9.总结 1.结构特性 Java ArrayList类使用 ...

  6. 【java集合框架源码剖析系列】java源码剖析之ArrayList

    注:博主java集合框架源码剖析系列的源码全部基于JDK1.8.0版本. 本博客将从源码角度带领大家学习关于ArrayList的知识. 一ArrayList类的定义: public class Arr ...

  7. 【java集合框架源码剖析系列】java源码剖析之HashMap

    前言:之所以打算写java集合框架源码剖析系列博客是因为自己反思了一下阿里内推一面的失败(估计没过,因为写此博客已距阿里巴巴一面一个星期),当时面试完之后感觉自己回答的挺好的,而且据面试官最后说的这几 ...

  8. 【java集合框架源码剖析系列】java源码剖析之TreeSet

    本博客将从源码的角度带领大家学习TreeSet相关的知识. 一TreeSet类的定义: public class TreeSet<E> extends AbstractSet<E&g ...

  9. 【java集合框架源码剖析系列】java源码剖析之HashSet

    注:博主java集合框架源码剖析系列的源码全部基于JDK1.8.0版本.本博客将从源码角度带领大家学习关于HashSet的知识. 一HashSet的定义: public class HashSet&l ...

  10. 【java集合框架源码剖析系列】java源码剖析之TreeMap

    注:博主java集合框架源码剖析系列的源码全部基于JDK1.8.0版本.本博客将从源码角度带领大家学习关于TreeMap的知识. 一TreeMap的定义: public class TreeMap&l ...

随机推荐

  1. userdel 用户名 出现“用户**目前已登录”

    userdel 用户名 出现“用户**目前已登录” 今天在删除用户账号的时候,发现一个奇怪现象,即: userdel: user newname is currently logged in 相关命令 ...

  2. python的字符串分片

    s = "abcdefg" 对于这样一个字符串进行操作.分片格式为: s[i:j:k] 其中,i和j分别代表子串的起始和末尾:-1则代表倒数第一个元素,同理-2代表倒数第二个元素. ...

  3. rem布局js实现

    (function(designWidth, maxWidth) { var doc = document, win = window; var docEl = doc.documentElement ...

  4. swfupload文件上传配置文件大小

    在配置文件中加入: <system.web>         <httpRuntime executionTimeout="36000" maxRequestLe ...

  5. Mysql 获取表属性

    获取表字段信息: select column_name from information_schema.COLUMNS where table_name='表名' nformation_schema. ...

  6. VS2003在解决方案范围内搜索卡死问题的解决

    在Win7系统上使用VS2003的时候,在解决方案范围内搜索某个内容的时候,VS会卡死. 这是一个兼容性问题,Win7系统对VS2003的兼容性不好, 网上有人讲了一种解决方法是: 在vs2003的图 ...

  7. DOS 格式化日期时间输出

    if "%date:~5,2%" lss "10" (set mm=0%date:~6,1%) else (set mm=%date:~5,2%)if &quo ...

  8. Django--URL(路由层)

    一.django 静态文件配置 在配置文件中settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR ...

  9. APP-8.2-Postman应用

    用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具.今天给大家介绍的这款网页调试工具不仅可以 ...

  10. maven创建项目,打包出可执行Jar

    官网参考 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 配置多种打包方式 这个例子也不错 https://bl ...