呵呵!你在判断是否hasNext()后,没有往后推移,就一直在这死循环了!当然有问题了。
如果你就在while循环中用iter.next(),会发生ConcurrentModificationException,具体的原因你可以看看ConcurrentModificationException的API文档,你可以这样做。
HashSet hs = new HashSet();
hs.add("数字:\n");
for(int i = 0 ;i < 50 ;i ++){
Iterator iter = hs.iterator();
while(iter.hasNext()){
if (hs.add(Integer.toString(i))) {
break;
}
}
}
指定当成功地加入后,退出while.
如果你就在while循环中用iter.next(),会发生ConcurrentModificationException,具体的原因你可以看看ConcurrentModificationException的API文档,你可以这样做。
HashSet hs = new HashSet();
hs.add("数字:\n");
for(int i = 0 ;i < 50 ;i ++){
Iterator iter = hs.iterator();
while(iter.hasNext()){
if (hs.add(Integer.toString(i))) {
break;
}
}
}
指定当成功地加入后,退出while.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货