java中对集合对象list的几种循环访问
java中对集合对象list的几种循环访问的总结如下 1 经典的for循环
View Code
1 public static void main(String[] args) {
2
3 List<String> list = new ArrayList();
4 list.add("123");
5 list.add("java");
6 list.add("j2ee");
7 System.out.println("=========经典的for循环=======");
8 for(int i=0; i<list.size();i++){
9 System.out.println(list.get(i));
10 }
11 }
2 增强的for循环
View Code
1 public static void main(String[] args) {
2
3 List<String> list = new ArrayList();
4 list.add("123");
5 list.add("java");
6 list.add("j2ee");
7 System.out.println("=========Java1.6的for循环=======");
8 for(String s:list){
9 System.out.println(s);
10 }
11 }
3 Iterate的使用
View Code
1 public static void main(String[] args) {
2
3 List<String> list = new ArrayList();
4 list.add("123");
5 list.add("java");
6 list.add("j2ee");
7 System.out.println("=========Iterate循环=======");
8 Iterator<String> iter = list.iterator();
9 while(iter.hasNext()){
10 System.out.println(iter.next());
11 }
12
13 }
www.