您的位置:首页 > 编程语言 > Java开发

java.util.NoSuchElementException

2014-12-01 19:14 417 查看
long totaltime=0;

long calltime=System.currentTimeMillis();

System.out.println("println1:"+calltime);

Set<String> words=new HashSet<String>();

words.add("sour");

words.add("sweet");

words.add("bitter");

words.add("spicy");

Iterator<String> witer=words.iterator();

for(int i=0;i<20;i++)

{

System.out.println(witer.next());

}

long calltime2=System.currentTimeMillis();

System.out.println("calltime2:"+calltime2);

totaltime=calltime2-calltime;

System.out.println("totaltime:"+totaltime);

很简单的一段代码 ,什么原因报java.util.NoSuchElementException??

---这类的异常表明 表明枚举中没有更多的元素。

ok结果出来了 因为集合中只有4个元素,而循环了20;超过了长度。

--修改为i<4 之后 输出结果如下:

println1:1417432431871

sour

sweet

spicy

bitter

calltime2:1417432431872

totaltime:1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: