62,614
社区成员
发帖
与我相关
我的任务
分享
package com.nbgnuskin.collection;
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(n.getfirstName() + " " + n.getfirstName().length());
}
}
}
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);
}
}
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
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());
}
}
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());
}
}