Java ArrayList源码阅读

transient Object[]            elementData;//用来存储数据,使用transient关键字避免序列化
private int                   size;   //集合大小
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = new Object[0];//默认的空集合
private static final int      MAX_ARRAY_SIZE                    = 2147483639;//数据最大长度
新增:
public boolean add(E arg0) {
this.ensureCapacityInternal(this.size + 1);  // 集合大小加1
this.elementData[this.size++] = arg0;
return true;
}


public void add(int arg0, E arg1) { //指定位置新增元素


this.rangeCheckForAdd(arg0);
this.ensureCapacityInternal(this.size + 1);
System.arraycopy(this.elementData, arg0, this.elementData, arg0 + 1, this.size - arg0);
this.elementData[arg0] = arg1;
++this.size;
}


//用来设置需要开辟的空间大小
private void ensureCapacityInternal(int arg0) {
    // 当前集合的内容与默认的内容相等时,取大的用来开辟集合容量
if (this.elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
arg0 = Math.max(10, arg0);
}

this.ensureExplicitCapacity(arg0);
}


private void ensureExplicitCapacity(int arg0) {


++this.modCount;
if (arg0 - this.elementData.length > 0) {  //如果需要存储的元素的个数大于当前数据的长度,则需要进行扩容
this.grow(arg0);
}
}
//数组扩容方法
private void grow(int arg0) {


int arg1 = this.elementData.length;
int arg2 = arg1 + (arg1 >> 1);  //默认1.5倍扩展
if (arg2 - arg0 < 0) {          //当扩展后的数组仍不足以放下所有数据时,将数组长度扩展到和需要放置的数据个数相同
arg2 = arg0;
}

if (arg2 - 2147483639 > 0) {
arg2 = hugeCapacity(arg0);
}

this.elementData = Arrays.copyOf(this.elementData, arg2);
}


private static int hugeCapacity(int arg) {


if (arg < 0) {
   throw new OutOfMemoryError();
}
else {
    return arg > 2147483639 ? Integer.MAX_VALUE : 2147483639;
}
}


获取集合大小:
public int size() {


return this.size;
}
判断集合是否为空:
public boolean isEmpty() {


return this.size == 0;
}
判断是否包含特定类:
public boolean contains(Object arg0) {


return this.indexOf(arg0) >= 0;
}
public int indexOf(Object arg0) {


int arg1;
if (arg0 == null) {//从数组的第一个元素开始遍历
for (arg1 = 0; arg1 < this.size; ++arg1) {
if (this.elementData[arg1] == null) {
return arg1;
}
}
}
else {
for (arg1 = 0; arg1 < this.size; ++arg1) {
if (arg0.equals(this.elementData[arg1])) {
return arg1;
}
}
}

return -1;
}


清空列表:
public void clear() {


++this.modCount;
//循环处理,将数组对应的每个元素设置为null
for (int arg0 = 0; arg0 < this.size; ++arg0) {
this.elementData[arg0] = null;
}

this.size = 0;
}


删除元素:
public E remove(int arg0) { //根据索引删除,返回的是删除的元素


this.rangeCheck(arg0);
++this.modCount;
Object arg1 = this.elementData(arg0);
int arg2 = this.size - arg0 - 1;
if (arg2 > 0) {
System.arraycopy(this.elementData, arg0 + 1, this.elementData, arg0, arg2);   //System.arraycopy为native方法,相比于Arrays.copyOf 效率更高
}

this.elementData[--this.size] = null;
return arg1;
}
public boolean remove(Object arg0) { //根据特定元素删除


int arg1;
if (arg0 == null) {
for (arg1 = 0; arg1 < this.size; ++arg1) {
if (this.elementData[arg1] == null) {
this.fastRemove(arg1);
return true;
}
}
}
else {
for (arg1 = 0; arg1 < this.size; ++arg1) {
if (arg0.equals(this.elementData[arg1])) {
this.fastRemove(arg1);
return true;
}
}
}

return false;
}
private void fastRemove(int arg0) {


++this.modCount;
int arg1 = this.size - arg0 - 1;
if (arg1 > 0) {
System.arraycopy(this.elementData, arg0 + 1, this.elementData, arg0, arg1);
}

this.elementData[--this.size] = null;
}


public boolean removeAll(Collection<?> arg0) {  //批量删除


Objects.requireNonNull(arg0);
return this.batchRemove(arg0, false);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值