import java.util.Collections;このプログラムを実行するとどうなるか見てみましょう:
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
public class parcours_hashset {
public static void main(String[] args) {
HashSet<文字列>hset = new HashSet<文字列>();
hset.add("he1");
hset.add("he2");
hset.add("he3");
/*高度な For ループ*/
System.out.println("高度な For ループ");
for(文字列s:hset)
System.out.println(s);
/*while+イテレータループ*/
System.out.println("while+イテレータループ");
Iteratorit = hset.iterator();
while(it.hasNext())
System.out.println(it.next());
/*列挙*/
System.out.println("while+列挙ループ");
// Enumeration
Enumerationenumeration = Collections.enumeration(hset);
// HashSet
while(enumeration.hasMoreElements())
System.out.println(enumeration.nextElement());
}
}
高度なThe iterator() は、HashSet または Set の要素を介してイテレータを取得するために使用されます。要素は混合され、特定の順序なしで返されます.
he3
he1
he2
While+Iterator
he3
he1
he2
While+Enumeration Loop
he3
he1
he2
Please disable your ad blocker and refresh the window to use this website.