java中HashSet怎样遍历取出里面的值?给个例子~~

2025-01-07 03:01:42
推荐回答(2个)
回答1:

import java.util.HashSet;
import java.util.Iterator;

public class Test {
public static void main(String[] args) {
HashSet set = new HashSet();
for (int i = 0; i < 20; i++) {
set.add(i);
}
Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.println((Integer)iterator.next());
}
}

}

回答2:

用迭代器遍历