ArrayList遍历的4种方法

public class ArrayListDemo {

  public static void main(String args[]){

  List<String> list = new ArrayList<String>();

  list.add("luojiahui");

  list.add("luojiafeng");

  //方法1

  Iterator it1 = list.iterator();

  while(it1.hasNext()){

  System.out.println(it1.next());

  }

  //方法2 怪异!

  for(Iterator it2 = list.iterator();it2.hasNext();){

  System.out.println(it2.next());

  }

  //方法3

  for(String tmp:list){

  System.out.println(tmp);

  }

  //方法4

  for(int i = 0;i < list.size(); i ++){

  System.out.println(list.get(i));

  }

  }

  }

posted @ 2014-05-18 17:01  baoendemao  阅读(211)  评论(0编辑  收藏  举报