ITeye博客

打印集合元素的两种方法--迭代器与For-Each

alpha2009 · 2010-01-14 17:27 · し☆Java集合☆ · 编程语言 · Markdown

迭代打印集合中元素的方法:
Iterator it = set.iterator();//适用的大部分集合对象
  while(it.hasNext()){
    Object obj = it.next(); 
    if(obj instanceof String){
     String s = (String)obj;
     System.out.println(s);
    }
  }

for-each的写法
for(Object obj : set){//set为要集合对象名
      System.out.println(obj);
}
 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。