package Swxx; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Xx { public static void main(String[] args) { // TODO Auto-generated method stub method(); method1(); method2(); } public static void method(){ ArrayList<String> arr=new ArrayList<String>(); arr.add("aaa"); arr.add("aaa"); arr.add("bbb"); Object[] str=arr.toArray(); for(int i=0;i<str.length;i++){ System.out.println(str[i]); } for(Object obj:str){ System.out.println(obj); } } public static void method1(){ Collection<String> c=new ArrayList<String>(); c.add("123"); c.add("456"); // c.add(1); for (Object obj:c){ String str=(String)obj; System.out.println(obj); System.out.println(str.length()); } } public static void method2(){ Collection<String> c=new ArrayList<String>(); c.add("123"); c.add("456"); //迭代器遍历; //1、获取iterator迭代器对象 Iterator<String> it=c.iterator(); //2、hasNext判断集合中是否有元素 while(it.hasNext()){ //3、it.next返回集合中的下一个元素 //String str=it.next(); //System.out.println(str); System.out.println(it.next()); it.next(); //FOR循环的输出方式 } for(Iterator<String> it1=c.iterator();it1.hasNext();){ //String str=it.next(); //System.out.println(str); System.out.println(it1.next()); } } }