Java中 List的遍历
来源:互联网 发布:硬笔书法知乎 编辑:程序博客网 时间:2024/04/30 03:48
- import
java.util.Iterator; - import
java.util.List; - public
class ListTest { -
public static void main(String[] args) { -
List<Integer> list = ArrayList<Integer>(); -
list.add(1); -
list.add(2); -
list.add(3); -
(Iterator i = list.iterator(); i.hasNext();) -
System. // line 1 -
} - }
-
- public
class ListTest2 { -
static void main(String[] args){ -
List list = ArrayList(); -
list.add( Integer(100)); -
list.add( Float(150.60)); -
list.add( String( "abc")); -
i = list.iterator(); i.hasNext();) -
System. -
- //
List<Integer> list = new ArrayList<Integer>(); - //
list.add(1); - //
list.add(2); - //
list.add(3); - //
for(Integer i : list){ - //
System.out.println(i); // OK - //
} -
} - }
-
- //虽然List的Generic是Integer,但.next()返回的类型是Object
- public
class ListTest3 { -
public static void main(String[] args) { -
List<Integer> list = new ArrayList<Integer>(); -
list.add(1); -
list.add(2); -
list.add(3); -
for(Iterator i = list.iterator(); i.hasNext();){ -
Integer integerRef = (Integer) i.next(); //line 1 -
//Compile Error -
System.out.println(integerRef); // line 2 -
} -
List<Integer> list = ArrayList<Integer>(); -
list.add(1); -
list.add(2); -
list.add(3); -
(Iterator<Integer> i = list.iterator(); i.hasNext();) { -
Integer integerRef = i.next(); line 1 -
OK -
System. // line 2 -
} -
} - }
对List的遍历有三种方式
执行过程中会进行数据锁定, 性能稍差, 同时,如果你想在循环过程中去掉某个元素,只能调用it.remove方法, 不能使用list.remove方法, 否则一定出并发访问的错误.
第二种:
for(A a : list) {
.....
}
内部调用第一种, 换汤不换药, 这种循环方式还有其他限制, 不建议使用它
第三种:
for(int i=0; i<list.size(); i++) {
A a = list.get(i);
...
}
内部不锁定, 效率最高, 但是当写多线程时要考虑并发操作的问题
- Java中 List的遍历
- Java中 List的遍历
- java中List的遍历
- java中List的遍历
- java中List的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中List集合的遍历
- Java中 List的遍历及三种遍历方法
- java list的遍历
- Line Counter - Writing a Visual Studio 2005 & 2008 Add-In
- 坐得越久的人死得越快
- POJ 1637 Sightseeing tour (混合图的欧拉回路)
- Android Broadcast机制深入解析
- [转]vector.resize 与 vector.reserve的区别
- Java中 List的遍历
- [Sonia] Implementation of a Binary Tree Sort in C
- git 版本管理
- python基于http协议编程:httplib,urllib和urllib2
- Java获得磁盘盘符,并且分析电脑各个盘符中的大小
- artical on napi
- Linux系统调用--fcntl函数详解
- 编程珠玑代码调优习题12
- 要学会有算法写出代码