- package com.itheima;
- import java.util.*;
- public class ForeachDemo {
- public static void main(String[] args)
- {
- ArrayList<String> al= new ArrayList<String>();
- al.add("aaa");
- al.add("bbb");
- al.add("ccc");
- al.add("bbb");
- //迭代器
- Iterator it= al.iterator();
- while(it.hasNext())
- {
- System.out.println("Itrator::"+it.next());
- }
-
- //foreach
- for(String str : al)
- {
- System.out.println("str::"+str);
- }
-
- //for循环get()方法
- for(int x=0; x<al.size(); x++)
- {
- System.out.println("al("+x+")=="+al.get(x));
- }
- }
- }
复制代码 |