在数据结构中,讲解ArrayList类,首先数据结构是用来CRUD的。在此类中有充分的实现
和Vector不同,ArrayList中的操作不是线程安全的!所以,建议在单线程中才使用ArrayList,而在多线程中可以选择Vector或者CopyOnWriteArrayList。
ArrayList 实现了Cloneable接口,即覆盖了函数clone(),能被克隆。
ArrayList的常用方法
1.add
- 使用add方法向数组里列表中添加元素。
有2种方法:
public void add(int index,E element)
将指定的元素插入此列表中的指定位置。向右移动当前位于该位置的元素(如果有)以及所有后续元素(将其索引加 1)。
public boolean add(E e)
将指定的元素添加到此列表的尾部。
在java7之前,使用new ArrayList创建对象时,即使不add任何对象,在堆空间依然初始化了长度为10的Object数组,浪费空间
相当于性能不高的Vector
从Java7开始优化,new ArrayList创建对象时,其实底层创建一个空数组,在第一次调用add方法的时候,才会初始化数组为10。
2.remove
- 此方法用于删除一个元素,可以通过两种方式:根据索引删除或者根据具体的元素删除.
public E remove(int index)
移除此列表中指定位置上的元素。向左移动所有后续元素(将其索引减 1)。
public boolean remove(Object o)
移除此列表中首次出现的指定元素(如果存在)。如果列表不包含此元素,则列表不做改动。
3.removeRange
**protected void removeRange(int fromIndex,
int toIndex)**
移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。向左移动所有后续元素(减小其索引)。此调用将列表缩短了 (toIndex - fromIndex) 个元素。(如果 toIndex==fromIndex,则此操作无效。)
4.set
**public E set(int index,
E element)**
用指定的元素替代此列表中指定位置上的元素。
5.get
public E get(int index)
返回此列表中指定位置上的元素。
6.size
public int size()
返回此列表中的元素数。
7.clear
public void clear()
移除此列表中的所有元素。此调用返回后,列表将为空。 A.size()=0
8.isEmpty
public boolean isEmpty()
如果此列表中没有元素,则返回 true
9.indexOf
public int indexOf(Object o)
返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。
10.clone
public Object clone()
返回此 ArrayList 实例的浅表副本。(不复制这些元素本身。)
ArrayList的克隆函数,即是将全部元素克隆到一个数组中。
11.contains
public boolean contains(Object o)
如果此列表中包含指定的元素,则返回 true。
12.toArray
public Object[] toArray()
按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。
class ArrayListDemo
{
public static void main(String[] args)
{
Arraylist list=new ArrayList();
list.add(new integer(1));
list.add(new integer(2));
list.add(new integer(3));
list.add(new integer(4));
list.add(new integer(5));
Object[] in=list.toArray();
//不能将Object[]转换为integer[]
for(int i=0;i<in.length;i++){
System.out.println(((integer)in[i])).intvalue());
}
}
}
- ArrayList 本身接收的就是对象,所以用get()方法取出来的时候,需要把它装换成我们放进去的相应的类型。
如下:
class ArrayListDemo
{
public static void main(String[] args)
{
Arraylist list=new ArrayList();
list.add("hello");
list.add(new integer(2));
String s=(String)list.get(0);
integer in=(integer)list.get(1);
System.out.println(s);
System.out.println(in);
}
}
ArrayList的遍历
三种方法:
1.通过迭代器遍历(iterator)———最慢
Iterator it=list.iterator();
for (Iterator it=list.iterator();it.hasNext();) {
System.out.println(it.next());
}
2.随机访问,通过索引值去遍历——最快
integer value=null;
int size=list.size();
for(int i=0;i<size;i++){
value=(integer)list.get(i);
}
3.foreach遍历———一般(底层依旧是用的Iterator)
integer value=null;
for(integer inte:list){
value=inte;
}
ArrayList优点
- ArrayList底层以数组实现,是一种随机访问模式,所以查询和修改速度快
- 顺序添加元素时比较快
ArrayList缺点
- 删除和插入元素时,由于涉及到元素移位,所以比较耗费性能
ArrayList线程问题
ArrayList线程不安全,解决方法:
1. 继承ArrayList,然后重写或按需求编写自己的方法,加上synchronized,接着在synchronized修饰的方法中调用ArrayList.
2.List list=Collections. synchronizedList(new ArrayList()
synchronizedList(在Collections类中的方法)
public static List synchronizedList(List list)
返回指定列表支持的同步(线程安全的)列表。
ArrayList和Vector
List接口一共有三个实现类,分别是ArrayList、Vector和LinkedList
Vector与ArrayList一样,也是通过数组实现的,不同的是它支持线程的同步 Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的同步必然要影响性能,因此,ArrayList的性能比Vector好。
当Vector或ArrayList中的元素超过它的初始大小时,Vector会将它的容量翻倍,而ArrayList只增加50%的大小,这样,ArrayList就有利于节约内存空间。
本文有些知识点参考知乎上内容。
谢谢阅读!