ArrayList

来源:互联网 发布:权御天下知乎 编辑:程序博客网 时间:2024/06/16 14:48
java.util.ArrayList是一个用数组实现的List。能进行快速的随机访问,但是往列表中间插入和删除元素的时候比较慢。
  ListIterator用在反向遍历ArrayList的是合适的,但不要用它来插入和删除元素,因为向数组插入和删除元素比较慢。
  ArrayList的实现不是synchronized的. 如果多个线程访问一个ArrayList实例,并且至少有一个会对它进行修改的话,
  就必须对它使用外部
synchronized . (A structural modification is any operation that adds or deletes one or more elements, 
  or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) 
  This is typically accomplished by synchronizing on some object that naturally encapsulates the list. 
  If no such object exists, the list should be "wrapped" using the Collections.synchronizedList  method. 
  This is best done at creation time, to prevent accidental unsynchronized access to the list:
  示例如下:
   List list = Collections.synchronizedList(new ArrayList(...));
  注意1:ArrayList是线程序不安全的,不是同步的 。
  注意2:他支持List接口的所有操作。
  注意3:它允许null的元素。
  示例
   ArrayList<Integer> list=new ArrayList();
   
for(int i=1;i<6;i++)
    list.add(new Integer(i));
   list.add(null);
   ListIterator<Integer> lt=list.listIterator(list.size());
   
while(lt.hasPrevious())
    System.out.println(lt.previous());

  输出结果为,
  null
  5
  4
  3
  2
  1