Java集合框架系列——ArrayList

直通车:Java集合框架系列目录

本篇博文分为六个部分:

  1. 基本概念
  2. 继承关系
  3. 结构特点
  4. 常用API
  5. 遍历方式
  6. 代码示例

1.基本概念

ArrayList:一种可以动态增长和缩减的索引序列。

2.继承关系

这里写图片描述
ArrayList继承AbstractList,实现List。它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能。
ArrayList实现了RandmoAccess接口,即提供了随机访问功能,即可以通过元素的序号快速获取元素对象
ArrayList 实现了Cloneable接口,即覆盖了函数clone(),能被克隆。
ArrayList 实现java.io.Serializable接口,意即ArrayList支持序列化,能通过序列化传输。

3.结构特点

  • 底层机制基于动态数组实现
    当我们构造ArrayList时,若使用默认构造函数,则ArrayList的默认容量大小是10。
  • 随机访问、更新数据较快,增删元素较慢
  • 其空间浪费主要体现在list列表的结尾会预留一定的容量空间
    当ArrayList容量不足以容纳全部元素时,ArrayList会重新设置容量:新的容量=“(原始容量x3)/2+1”。具体的容量增长方式,请参考ensureCapacity()函数。
  • 不是线程安全的
    在并发访问时,如果在迭代同时有其他线程修改了 ArrayList, fail-fast 的迭代器 Iterator/ListIterator 会抛出ConcurrentModificationException异常。
    可以采取这样的方式:List list = Collections.synchronizedList(new ArrayList());
    或者考虑在单线程中才使用ArrayList,而在多线程中可以选择Vector或者CopyOnWriteArrayList。

4.常用API

boolean             add(E object)//添加一个数组对象  
boolean             addAll(Collection<? extends E> collection)//添加一个包含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)  

其中关于Collection<? extends E> c的疑问可以移步
Java中 ? extends E 和 ?super E 的区别

5.遍历方式

ArrayList支持3种遍历方式


1.通过迭代器遍历。即通过Iterator去遍历。

Integer value = null;
Iterator iter = list.iterator();
while (iter.hasNext()) {
    value = (Integer)iter.next();
}

2.通过索引遍历,随机访问。

Integer value = null;
int size = list.size();
for (int i=0; i<size; i++) {
    value = (Integer)list.get(i);        
}

3.for循环遍历。

Integer value = null;
for (Integer integ:list) {
    value = integ;
}

使用这三种方法遍历ArrayList时,

  • 通过索引随机访问效率最高
  • 使用迭代器效率最低

6.代码示例

import java.util.ArrayList;
import java.util.Arrays;


public class ArrayListTest {
    class Human{
    }

    class Male extends Human{   
    }

    public static void main(String[] args) {
        ArrayList<Integer> list1=new ArrayList<Integer>();
        list1.add(1); // Appends the specified element to the end of this list
        list1.add(2);
        list1.add(3);
        System.out.println(list1.size());
        System.out.println(list1);

        list1.add(2,4); // Inserts the specified element at the specified position in this list
        System.out.println(list1);

        list1.clear(); // Removes all of the elements from this list
        System.out.println(list1);

        list1.add(5);
        ArrayList<Integer> list2=new ArrayList<Integer>();
        list2.add(1);
        list2.add(2);
        list2.add(3);
        list2.addAll(list1); // Appends all of the elements in the specified collection to the end of this list
        System.out.println(list2);

        System.out.println(list2.contains(5)); // Judge if this list contains the specified element
        System.out.println(list2.get(2)); // Returns the element at the specified position in this list
        System.out.println(list2.indexOf(5)); // Returns the index of the first occurrence of the specified element, or -1 if this list doesn't contain

        list2.set(2, 10); // Replaces the element at the specified position in this list with the specified element
        System.out.println(list2);

        list2.remove(2); // Removes the element at the specified position in this list
        System.out.println(list2); 
        list2.remove(Integer.valueOf(1)); // Removes the first occurrence of the specified element from this list, if it is present
        System.out.println(list2);
        list2.removeAll(Arrays.asList(5)); // Removes from this list all of its elements that are contained in the specified collection
    }
}

输出结果:

3
[1, 2, 3]
[1, 2, 4, 3]
[]
[1, 2, 3, 5]
true
3
3
[1, 2, 10, 5]
[1, 2, 5]
[2, 5]

如有谬误或不完善之处,恳请斧正!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值