set集合的三種遍歷方式
阿新 • • 發佈:2019-01-25
1.利用迭代器進行 ,迭代遍歷:
Set<Object> sets = new HashSet<Object>();
Iterator<Object> it = set.iterator();
while (it.hasNext()) {
String str = (String)it.next();
System.out.println(str);
}
2.for迴圈遍歷:
for (String str : sets) {
System.out.println(str);
}
foreach迴圈遍歷:
for (Object obj: sets) {
if(obj instanceof Integer){
int aa= (Integer)obj;
}else if(obj instanceof String){
String aa = (String)obj
}
……..
Set<Object> sets = new HashSet<Object>();
Iterator<Object> it = set.iterator();
while (it.hasNext()) {
String str = (String)it.next();
System.out.println(str);
}
2.for迴圈遍歷:
for (String str : sets) {
System.out.println(str);
}
3.Set<Object> set = new HashSet<Object>();
for (Object obj: sets) {
if(obj instanceof Integer){
int aa= (Integer)obj;
}else if(obj instanceof String){
String aa = (String)obj
}
……..
}