关于iterator的remove方法的问题 本帖最后由 raylee2007 于 2013-01-09 16:22:00 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args) { Collection c = new HashSet(); c.add(new Name("a1","b1")); c.add(new Name("a2222","b2")); c.add(new Name("a3","b3")); c.add(new Name("a4","b4"));// while(i.hasNext())// {// Name n = (Name) i.next();// if(n.getfirstName().length() > 3)// {// i.remove();// }// System.out.println(n.getfirstName() + " " + n.getfirstName().length());// } for(Iterator i = c.iterator();i.hasNext();) { System.out.println(i.toString()); Name n = (Name)i.next(); if (n.getFirstName().length() > 3) { c.remove(i); //这一句,看区别 } System.out.println(n.getFirstName() + " " + n.getFirstName().length()); } } 谢谢了,也是不行,在iterator里面一般都是用他的对象的remove方法,而且应该是锁定collection里面的元素,collection的remove应该是不行的,测试了一下上面的代码,还是不行,也不知道哪里出了问题。 另外楼主注意一下规范。getFirstName();//自动生成的getfirstName();最后给改成了增强for的形式,你看下吧。只是参考而已。public static void main(String[] args) { Collection<Name> c = new HashSet<Name>(); c.add(new Name("a1","b1")); c.add(new Name("a2222","b2")); c.add(new Name("a3","b3")); c.add(new Name("a4","b4"));// while(i.hasNext())// {// Name n = (Name) i.next();// if(n.getfirstName().length() > 3)// {// i.remove();// }// System.out.println(n.getfirstName() + " " + n.getfirstName().length());// } /*for(Iterator i = c.iterator();i.hasNext();) { //System.out.println(i.toString()); Name n = (Name)i.next(); System.out.println(n.getFirstName()); if (n.getFirstName().length() > 3) { c.remove(i); } //System.out.println(n.getFirstName().toString() + " " + n.getFirstName().length()); }*/ for(Name o:c){ System.out.println(o.getFirstName()); if(o.getFirstName().length()>3){ c.remove(o); } } for(Object o:c){ System.out.println(o.toString()); } } 结果:a1a4a3a2222Name [firstName=a1, s2=b1]Name [firstName=a4, s2=b4]Name [firstName=a3, s2=b3] 已经起作用了! public static void main(String[] args) { // TODO Auto-generated method stub Collection c = new HashSet(); c.add(new Name("a1", "b1")); c.add(new Name("a2222", "b2")); c.add(new Name("a3", "b3")); c.add(new Name("a4", "b4")); System.out.println("操作前集合中的元素个数:" + c.size()); for (Iterator i = c.iterator(); i.hasNext();) { Name n = (Name) i.next(); if (n.getFirstName().length() > 3) { i.remove(); } System.out.println(n.getFirstName() + " " + n.getFirstName().length()); } System.out.println("操作后集合中的元素个数:" + c.size()); }打印的结果是:4a4 2a1 2a2222 5a3 23 谢谢上面的各位,我放打印的地方不对最后的代码如下,就正常了import java.util.*;public class IteratorTest { public IteratorTest() { // TODO Auto-generated constructor stub } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Collection c = new HashSet(); c.add(new Name("a1","b1")); c.add(new Name("a2222","b2")); c.add(new Name("a3","b3")); c.add(new Name("a4","b4"));// while(i.hasNext())// {// Name n = (Name) i.next();// if(n.getfirstName().length() > 3)// {// i.remove();// }// System.out.println(n.getfirstName() + " " + n.getfirstName().length());// } for(Iterator i = c.iterator();i.hasNext();) { Name n = (Name)i.next(); if (n.getfirstName().length() > 3) { i.remove(); } } System.out.println(c); }} 异常机制与i++的一点疑问 一道有趣的线程问题! java 中哪个类能实现IDE环境中的语法着色功能? 个位兄弟有一事相求!急(Hashtable 的问题) 按钮事件 关于一个简单问题,求详细解释,等待中,解答后立即给分! 怎样判断resultset中有多少条记录? 怎么样判断我运行的程序的绝对路径? apache + ssl 问题,希望各位前辈帮助 java中子类调用父类中构造函数的this所属是什么? Integer和 String 也一样在栈中? 跪求sun.audio包!!!
public static void main(String[] args) {
Collection c = new HashSet();
c.add(new Name("a1","b1"));
c.add(new Name("a2222","b2"));
c.add(new Name("a3","b3"));
c.add(new Name("a4","b4"));
// while(i.hasNext())
// {
// Name n = (Name) i.next();
// if(n.getfirstName().length() > 3)
// {
// i.remove();
// }
// System.out.println(n.getfirstName() + " " + n.getfirstName().length());
// }
for(Iterator i = c.iterator();i.hasNext();)
{
System.out.println(i.toString());
Name n = (Name)i.next();
if (n.getFirstName().length() > 3) {
c.remove(i); //这一句,看区别 }
System.out.println(n.getFirstName() + " " + n.getFirstName().length());
}
}
getFirstName();//自动生成的
getfirstName();
最后给改成了增强for的形式,你看下吧。
只是参考而已。public static void main(String[] args) {
Collection<Name> c = new HashSet<Name>();
c.add(new Name("a1","b1"));
c.add(new Name("a2222","b2"));
c.add(new Name("a3","b3"));
c.add(new Name("a4","b4"));
// while(i.hasNext())
// {
// Name n = (Name) i.next();
// if(n.getfirstName().length() > 3)
// {
// i.remove();
// }
// System.out.println(n.getfirstName() + " " + n.getfirstName().length());
// }
/*for(Iterator i = c.iterator();i.hasNext();)
{
//System.out.println(i.toString());
Name n = (Name)i.next();
System.out.println(n.getFirstName());
if (n.getFirstName().length() > 3) {
c.remove(i);
}
//System.out.println(n.getFirstName().toString() + " " + n.getFirstName().length());
}*/
for(Name o:c){
System.out.println(o.getFirstName());
if(o.getFirstName().length()>3){
c.remove(o);
}
}
for(Object o:c){
System.out.println(o.toString());
}
}
a1
a4
a3
a2222
Name [firstName=a1, s2=b1]
Name [firstName=a4, s2=b4]
Name [firstName=a3, s2=b3]
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c = new HashSet();
c.add(new Name("a1", "b1"));
c.add(new Name("a2222", "b2"));
c.add(new Name("a3", "b3"));
c.add(new Name("a4", "b4"));
System.out.println("操作前集合中的元素个数:" + c.size());
for (Iterator i = c.iterator(); i.hasNext();) {
Name n = (Name) i.next();
if (n.getFirstName().length() > 3) {
i.remove();
}
System.out.println(n.getFirstName() + " "
+ n.getFirstName().length());
}
System.out.println("操作后集合中的元素个数:" + c.size());
}
打印的结果是:
4
a4 2
a1 2
a2222 5
a3 2
3
谢谢上面的各位,我放打印的地方不对
最后的代码如下,就正常了
import java.util.*;public class IteratorTest {
public IteratorTest() {
// TODO Auto-generated constructor stub
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c = new HashSet();
c.add(new Name("a1","b1"));
c.add(new Name("a2222","b2"));
c.add(new Name("a3","b3"));
c.add(new Name("a4","b4"));
// while(i.hasNext())
// {
// Name n = (Name) i.next();
// if(n.getfirstName().length() > 3)
// {
// i.remove();
// }
// System.out.println(n.getfirstName() + " " + n.getfirstName().length());
// }
for(Iterator i = c.iterator();i.hasNext();)
{
Name n = (Name)i.next();
if (n.getfirstName().length() > 3) {
i.remove();
}
}
System.out.println(c);
}}