【Java基础】Java8集合[ArrayList 之 属性](底层原理+源码分析)

CSDN话题挑战赛第2期
参赛话题:学习笔记

在这里插入图片描述


前言

Java集合类可以用于存储多个对象,还可以保存具有映射关系(Key-Value)的关联数组

可以说Java集合就像是一个容器,可以动态地把多个对象引入到容器当中。

而在这篇文章中,我们将讲解集合中ArrayList实现类的属性
集合主要分为Collection接口Map接口两类,而ArrayList就是Collection接口的子接口:List接口的一个实现类…


一、ArrayList概述

我们可以看一下,ArrayList实现类源码中的第一段注释:

Resizable-array implementation of the List interface. Implements all
optional list operations, and permits all elements, including null. In
addition to implementing the List interface, this class provides
methods to manipulate the size of the array that is used internally to
store the list. (This class is roughly equivalent to Vector, except
that it is unsynchronized.)

注释大义:
ArrayList 是一个动态的数组,实现了所有的可选操作(也就是实现了List接口以及相关的所有方法),它允许所有元素插入,其中包括null。
除了实现了List接口,该类还提供了一些方法用于操作内部存储数组的大小。(这个类大致相当于Vector,不同的是ArrayList线程不同步,而Vector是线程同步的)

源码已经告诉我们,ArrayList集合的底层,就是动态的数组,可以存储包括null在内的所有元素…


二、ArrayList属性

1.序列号 serialVersionUID

serialVersionUID代表序列版本号,用来表明类的不同版本间的兼容性

属性源码

private static final long serialVersionUID = 8683452581122892189L;


2.默认容量 DEFAULT_CAPACITY

ArrayList集合的默认容量为10…

属性源码

    /**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10;


3.空数组常量 EMPTY_ELEMENTDATA

属性源码

 /**
     * Shared empty array instance used for empty instances.
     */
    private static final Object[] EMPTY_ELEMENTDATA = {};


4.默认空数组常量

用于默认大小的空实例。将默认空数组常量DEFAULTCAPACITY_EMPTY_ELEMENTDATA与空数组常量EMPTY_ELEMENTDATA区分开来,我们就可以知道数组是在扩容到什么时候,第一个元素被添加进来的。

空数组列表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 = {};


5.存储数组 elementData

通过对下列源码的理解,我们可以知道的是:ArrayList集合的底层实现,使用的是一个Object类型的数组…

这个属性应该是ArrayList实现类中最重要的属性了,该实现类的方法,基本是建立在存储数据的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



6.数组长度 size

属性源码

  /**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
    private int size;


7.容量上限 MAX_ARRAY_SIZE

MAX_ARRAY_SIZE代表的是数组的最大上限,数组的长度必须<=MAX_ARRAY_SIZE

属性源码

    /**
     * The maximum size of array to allocate.
     * Some VMs reserve some header words in an array.
     * Attempts to allocate larger arrays may result in
     * OutOfMemoryError: Requested array size exceeds VM limit
     */
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

集合ArrayList的属性就讲解道这里啦,下一篇文章来讲解集合ArrayList的方法…
在这里插入图片描述

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.29.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值