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
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(...));
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
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- javaweb网上书店之表单提交---文本框被disabled后request.form不能得到其值
- 关于接口interface和new关键字
- android map开发2 移动指定位置
- 在lineEdit中只能输入字符,数字和空格的写法
- 生命是个飘移的过程
- ArrayList
- 实例构造函数
- view slide as the user slides finger on the screen?
- Eclipse快捷键大全(转载)
- LinkedList
- VC解析XML--使用CMarkup类解析XML
- 浅谈Base64编码
- 我的csdn第一课
- OT源代码的分析,OrtHello 迟早攻破你 (十三)ortHello的代码执行顺序