1.for循环方式(Set集合不能使用,因为Set是无序的没有索引)
for (int i = 0; i < list.size(); i++) {
Object o = list.get(i);
System.out.println(o);
}
2.foreach
String[] QQ={"26578","34535353","756345234","757445676","34535353","26578"};
HashSet<String> setQQ=new HashSet<>();
for (String e:QQ
) {
setQQ.add(e);
}
for (String qq:setQQ//使用Set可以去除重复数据
) {
System.out.println(qq);
}
3.迭代器iterator
iterator.hasNext();//是否有下一个元素?
iterator.next();//移动迭代器,取出元素
while循环迭代器
Iterator<String> iterator=setQQ.iterator();
while (iterator.hasNext()){
String str=iterator.next();
System.out.println(str);
}
for循环迭代器
for (Iterator<String> stringIterator = setQQ.iterator(); stringIterator.hasNext(); ) {
String next = stringIterator.next();
System.out.println(next);
}