ArrayList是一个数组队列,相当于是一个动态的数组,可以实现增删查改和遍历的功能(继承于AbstractList类),可以随机访问(实现了RandmoAccess接口),而且可以根据元素的序列号,快速去获取对象。
api接口(摘自http://www.cnblogs.com/skywang12345/p/3308556.html)
// 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)
ArrayList支持3种遍历方式,通过迭代器遍历;随机访问,通过索引值去访问;for循环遍历
import java.util.*; import java.util.concurrent.*; public class Test { public static void main(String[]args){ List list=new ArrayList(); for(int i=0;i<99999;i++){ list.add(i); } iteratorThroughRandomAccess(list); iteratorThroughFor2(list); iteratorThroughIterator(list); } private static void isRandomAccessSupported(List list){ if(list instanceof RandomAccess){ System.out.println("RandomAccess implemented!"); }else{ System.out.println("RandomAccess not implemented!"); } } //随机进行访问 private static void iteratorThroughRandomAccess(List list){ long startTime; long endTime; startTime=System.currentTimeMillis(); for(int i=0;i<list.size();i++){ list.get(i); } endTime=System.currentTimeMillis(); long interval=endTime-startTime; System.out.println(interval+"ms"); } //for循环进行遍历 private static void iteratorThroughFor2(List list){ long startTime; long endTime; startTime=System.currentTimeMillis(); for(Object obj:list); endTime=System.currentTimeMillis(); long interval=endTime-startTime; System.out.println(interval+"ms"); } //通过迭代器进行访问 private static void iteratorThroughIterator(List list){ long startTime; long endTime; startTime=System.currentTimeMillis(); for(Iterator iter=list.iterator();iter.hasNext();){ iter.next(); } endTime=System.currentTimeMillis(); long interval=endTime-startTime; System.out.println(interval+"ms"); } }
由此得出随机访问效率最高。
import java.util.*; public class Test { public static void main(String[]args){ List list=new ArrayList(); //添加元素 list.add("1"); list.add("2"); list.add("3"); list.add("4"); //在指定位置添加元素 list.add(0, "5"); //输出第一个元素 System.out.println("first"+list.get(0)); //remove element System.out.println("ArrayList‘size"+list.size()); //判断是否有元素4 System.out.println("contains 4"+list.contains("4")); for(Iterator iter=list.iterator();iter.hasNext();) System.out.println("next is "+iter.next()); //转化 String[]st=(String[])list.toArray(new String[0]); for(String str:st) System.out.println("str:"+st); //清除 list.clear(); //判断是否为空 System.out.println(list.isEmpty()); } }
常见的功能!
时间: 2024-10-20 12:17:46