Java ArrayList

Java ArrayList

介绍

ArrayList 内部维护了一个

1
transient Object[] elementData;

调用构造函数的时候把内部的静态空数组赋值给elementData.或者根据长度赋值一个新的数组

add,get等操作通过调用elementData来实现, 如果elementData的长度不足,则经过一连串跳转之后调用native方法将数组复制到一个size比原先大1的新数组中.

ArrayList

1
2
3
ArrayList<E> extends AbstractList<E> implements List<E> ,RandomAccess,Cloneable,java.io.Serializable{
...
}

ArrayList类实现了4个interface

  • List
  • RandomAccess
  • Cloneable
  • java.io.Serializable

除了List接口之外,其余的三个接口都是空的

1
2
3
4
5
6
7
8
9
10
public interface RandomAccess{

}

public interface Cloneable{

}

public interface Serializable {
}

RandomAccess

表示实现了该接口的类使用for循环迭代比使用迭代器更快

Cloneable

实现了该接口的类就需要重写Object.clone()方法

API原文为:

By convention, classes that implement this interface should override
Object.clone (which is protected) with a public method.

Serializable

表示该类可序列化,需要自定义一个序列号的uid作为识别

1
private static final long serialVersionUID = 8683452581122892189L;
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2020 Kfdykme
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信