java使用while或for遍历Collection对象

深入for,while,foreach遍历 +Iterator

publicclass Test3 {
      publicstaticvoid main(String[] args) {
           List l = new ArrayList();
           l.add("世界你好!");
           l.add("上海");
           for (int i = 0; i < l.size(); i++) { // 普通的for循环取出集合中的元素
                 System.out.print(l.get(i));
 
           }
           System.out.println();
           for (Object o : l) { // 增强for循环取出集合中的元素
                 System.out.print(o);
           }
           System.out.println();
           for (Iterator it = l.iterator(); it.hasNext();) { // 利用迭代器取出集合中的元素
 
                 System.out.print(it.next());
 
           }
 
           System.out.println();
           Iterator it = l.iterator(); //利用迭代器取出集合中的元素
           while (it.hasNext()) {
                 System.out.print(it.next());
 
           }
 
      }
 
}
 
 
 
 
 

posted on 2016-07-22 12:15  岁月温柔  阅读(498)  评论(0)    收藏  举报

导航