-- 简单问题 --

bzg119 2002-06-25 11:17:41
HashSet hs = new HashSet();
hs.add("数字:\n");
for(int i = 0 ;i < 50 ;i ++){
Iterator iter = hs.iterator();
while(iter.hasNext()){
hs.add(Integer.toString(i));
}
}
执行时出现问题,WHY?
一个外部的HashSet,循环50次,每次循环遍历此HashSet,同时将HashSet增加一项。为什么我改变的是循环的外部,每次循环都得到一个新的Iterator,为何出错?有何解决的办法没有?

请各位老师指点迷津?立即打分!
...全文
27 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bzg119 2002-06-27
  • 打赏
  • 举报
回复
谢谢你!你讲得很对,我因而解决了问题。
shihb 2002-06-25
  • 打赏
  • 举报
回复
呵呵!你在判断是否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.
bzg119 2002-06-25
  • 打赏
  • 举报
回复
在线等待!
C++大学教程(目录) 第1章 计算机与C++编程简介-------------------------------------------------1 1.1 简介--------------------------------------------------------------1 1.2 什么是计算机------------------------------------------------------3 1.3 计算机组成--------------------------------------------------------3 1.4 操作系统的变革----------------------------------------------------4 1.5 个人计算、分布式计算与客户/服务器计算-----------------------------4 1.6 机器语言、汇编语言和高级语言--------------------------------------5 1.7 C语言与C++的历史--------------------------------------------------6 1.8 C++标准库---------------------------------------------------------7 1.9 Java、Internet与万维网--------------------------------------------7 1.10 其它高级语言------------------------------------------------------8 1.11 结构化编程--------------------------------------------------------8 1.12 典型C++环境基础---------------------------------------------------8 1.13 C++与本书的一般说明----------------------------------------------10 1.14 C++编程简介------------------------------------------------------11 1.15 简单程序:打印一行文本-------------------------------------------12 1.16 简单程序:两个整数相加-------------------------------------------15 1.17 内存的概念-------------------------------------------------------18 1.18 算术运算---------------------------------------------------------19 1.19 判断:相等与关系运算---------------------------------------------21 1.20 新型头文件与名字空间---------------------------------------------24 1.21 有关对象的思考---------------------------------------------------25 小结------------------------------------------------------------------27 术语------------------------------------------------------------------29 自测练习--------------------------------------------------------------31 自测练习答案----------------------------------------------------------33 练习------------------------------------------------------------------34 第2章 控制结构-----------------------------------------------------------38 2.1 简
-------------------------------------------------------------------------------- 飞鸽传书 (IP Messenger) Ver2.06 Copyright (C) 1996-2004 SHIROUZU Hiroaki All Rights Reserved. -------------------------------------------------------------------------------- 目录: 1. 软件简介 2. 许可协议 3. 系统要求 4. 使用说明 5. 相关信息 6. 广域网设置(广播设置) 7. 补充 8. 支持 9. 更新历史 10. 感谢 ================================================================================ 重要说明: comctl32.dll(公共控件) 要求 4.71 或以上版本 更多信息请参见 "系统要求" ================================================================================ -------------------------------------------------------------------------------- 1. 软件简介 - IPMsg 是一款局域网内即时通信软件, 基于 TCP/IP(UDP). 可运行于多种操作平台(Win/Mac/UNIX/Java), 并实现跨平台信息交流. - 不需要服务器支持. - 支持文件/文件夹的传送 (2.00版以上) - 通讯数据采用 RSA/Blofish 加密 (2.00版以上) - 十分小巧, 简单易用, 而且你可以完全免费使用它 - 目前已有的版本包括: Win32, Win16, MacOS, MacOSX, X11, GTK, GNOME, Java 等, 并且公开源代码. 请查看以下地址以获得相关信息: http://www.ipmsg.org/ -------------------------------------------------------------------------------- 2. 许可协议 (BSD License) Copyright (c) 1996-2004 SHIROUZU Hiroaki All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the SHIROUZU Hiroaki nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRI

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧