ArrayList是实现List接口的,底层采用数组实现。
ArrayList 实现了Cloneable接口,即覆盖了函数clone(),能被克隆。
ArrayList 实现java.io.Serializable接口,这意味着ArrayList支持序列化,能通过序列化去传输。
ArrayList 去重
利用HashSet里面的元素不可重复
利用list里面contains方法比较是否存在去重
第一种方法
ArrayList arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(1);
arrayList.add(3);
arrayList.add(2);
arrayList.add(3);
arrayList= new ArrayList<>(new HashSet<>(arrayList));for (int i=0;i
printlns("arrayList ["+ i +"] = "+arrayList.get(i));
}
运行结果
arrayList [0] = 1arrayList [1] = 2arrayList [2] = 3
第二种方法//list:集合,name:元素
ArrayUtils.contains(list, name)
ArrayList的API
//Collection中定义的API
booleanadd(E object)boolean addAll(Collection extends E>collection)voidclear()booleancontains(Object object)boolean containsAll(Collection>collection)booleanequals(Object object)inthashCode()booleanisEmpty()
Iteratoriterator()booleanremove(Object object)boolean removeAll(Collection>collection)boolean retainAll(Collection>collection)intsize()T[] toArray(T[] array)
Object[] toArray()//AbstractCollection中定义的API
void add(intlocation, E object)boolean addAll(int location, Collection extends E>collection)
E get(intlocation)intindexOf(Object object)intlastIndexOf(Object object)
ListIterator listIterator(intlocation)
ListIteratorlistIterator()
E remove(intlocation)
E set(intlocation, E object)
List subList(int start, intend)//ArrayList新增的API
Object clone()void ensureCapacity(intminimumCapacity)voidtrimToSize()void removeRange(int fromIndex, int toIndex)
JDK6
1、构造函数
ArrayList提供了三个构造函数:
ArrayList():默认构造函数,提供初始容量为10的空列表。
ArrayList(int initialCapacity):构造一个具有指定初始容量的空列表。
ArrayList(Collection extends E> c):构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的。
2 、新增
ArrayList提供了add(E e)、add(int index, E element)、addAll(Collection extends E> c)、addAll(int index, Collection extends E> c)、set(int index, E element)这个五个方法来实现ArrayList增加。
add(E e):将指定的元素添加到此列表的尾部。
add(int index, E element):将指定的元素插入此列表中的指定位置。
addAll(Collection extends E> c):按照指定 collection 的迭代器所返回的元素顺序,将该 collection 中的所有元素添加到此列表的尾部。
addAll(int index, Collection extends E> c):从指定的位置开始,将指定 collection 中的所有元素插入到此列表中。
set(int index, E element):用指定的元素替代此列表中指定位置上的元素。
3、删除
ArrayList提供了remove(int index)、remove(Object o)、removeRange(int fromIndex, int toIndex)、removeAll()四个方法进行元素的删除。
remove(int index):移除此列表中指定位置上的元素。
remove(Object o):移除此列表中首次出现的指定元素(如果存在)。
removeRange(int fromIndex, int toIndex):移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。
removeAll():是继承自AbstractCollection的方法,ArrayList本身并没有提供实现。
Iterator iter = list.iterator();
while (iter.hasNext()) {
String item = iter.next();
if (item.equals("aa")) {
iter.remove();
}
}
3、修改
set(int index, E element) :set(1, "b") 设置第2个元素为b。
4、查找
查找元素有Contains()、IndexOf()、LastIndexOf()3中方法
al.Contains(object obj);//查找数组中是否有obj元素,存在返回true;
IndexOf()有两个重载方法 起用法如下:
1)、al.IndexOf(object obj);//从0开始查找obj元素,只第一个obj元素,并返回起在数组中的位置,如果不存在,返回-1;
2)、al.IndexOf(object obj,int startIndex); //从startIndex开始查找obj元素,只第一个obj元素,并返回起在数组中的位置,
3)、al.IndexOf(object obj,int startIndex,int count); 从startIndex开始想后查找count个元素,如果存在obj元素,则返回其在数组中的位置
al.LastIndexOf()方法与IndexOf()用法相同,它也有两个重载,其不同的是,LastIndexOf(obj)是查找要obj最后出现的位置
ArrayList提供了get(int index)用读取ArrayList中的元素。由于ArrayList是动态数组,所以我们完全可以根据下标来获取ArrayList中的元素,而且速度还比较快,故ArrayList长于随机访问。
contains(Object o):此集合中是否包含某一个元素。
ArrayList支持3种遍历方式
(01) 第一种,通过迭代器遍历。即通过Iterator去遍历。
Integer value = null;
Iterator iter=list.iterator();while(iter.hasNext()) {
value=(Integer)iter.next();
}
(02) 第二种,随机访问,通过索引值去遍历。
由于ArrayList实现了RandomAccess接口,它支持通过索引值去随机访问元素。
Integer value = null;int size =list.size();for (int i=0; i
value=(Integer)list.get(i);
}
(03) 第三种,for循环遍历。如下:
Integer value = null;for(Integer integ:list) {
value=integ;
}
遍历ArrayList时,使用随机访问(即,通过索引序号访问)效率最高,而使用迭代器的效率最低!
// 将ArrayList转换为数组
String[] arr = (String[])list.toArray(new String[0]);
for (String str:arr)
System.out.println("str: "+ str);
// 清空ArrayList
list.clear();
// 判断ArrayList是否为空
System.out.println("ArrayList is empty: "+ list.isEmpty());
/**
* 无序集合转换为有序列表
*
* @param set
* @return
*/
public static List setSortToList(Set set) {
List setList = new ArrayList(set);
Collections.sort(setList, new Comparator() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
return setList;
}