Java ArrayList详细介绍和使用示例(正在整理)

对ArrayList的整体认识

ArrayList是一个数组队列,相当于动态数组。与Java中的数组相比,它的容量能动态增长。它继承了AbstractList,实现了List,RandomAccess,Cloneable,java.io.Serializable这些接口。

ArrayList继承了AbstractList,实现了List.它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能。

 ArrayList实现了RandomAccess接口,即提供了随机访问功能,RandomAccess是java中用来被List实现,为List提供快速访问功能的。在ArrayList中,我们即可以通过元素的序号快速获取元素对象,这就是快速随机访问。稍后,我们会比较List的快速随机访问和通过Iterator迭代器访问的效率。

ArrayList实现了Cloneable接口,即覆盖了函数clone(),能被克隆。

 ArrayList实现java.io.Serializable接口,这意味着ArrayList支持序列化,能通过序列化去传输。

和Vector不同,ArrayList中的操作不是线程安全的,所以,建议在单线程中才使用ArrayList,而在多线程中可以选择Vector或则CopyOnWriteArrayList.

 ArrayList构造函数

// 默认构造函数
ArrayList()

// capacity是ArrayList的默认容量大小。当由于增加数据导致容量不足时,容量会添加上一次容量大小的一半。
ArrayList(int capacity)

// 创建一个包含collection的ArrayList
ArrayList(Collection<? extends E> collection)

  

 ArrayList的API

扫描二维码关注公众号,回复: 1686298 查看本文章
// Collection中定义的API
boolean             add(E object)
boolean             addAll(Collection<? extends E> collection)
void                clear()
boolean             contains(Object object)
boolean             containsAll(Collection<?> collection)
boolean             equals(Object object)
int                 hashCode()
boolean             isEmpty()
Iterator<E>         iterator()
boolean             remove(Object object)
boolean             removeAll(Collection<?> collection)
boolean             retainAll(Collection<?> collection)
int                 size()
<T> T[]             toArray(T[] array)
Object[]            toArray()
// AbstractCollection中定义的API
void                add(int location, E object)
boolean             addAll(int location, Collection<? extends E> collection)
E                   get(int location)
int                 indexOf(Object object)
int                 lastIndexOf(Object object)
ListIterator<E>     listIterator(int location)
ListIterator<E>     listIterator()
E                   remove(int location)
E                   set(int location, E object)
List<E>             subList(int start, int end)
// ArrayList新增的API
Object               clone()
void                 ensureCapacity(int minimumCapacity)
void                 trimToSize()
void                 removeRange(int fromIndex, int toIndex)

  

http://www.cnblogs.com/skywang12345/p/3308556.html

猜你喜欢

转载自www.cnblogs.com/baxianhua/p/9211265.html