/*
* Iterator 接口的使用 所有的Collection接口类都有一个iterator方法
*以返回一个实现了Iterator接口的对象
*/
import java.util.*;
class Name
{
private String firstname;
private String lastname;
Name(String firstname,String lastname)
{
this.firstname = firstname;
this.lastname = lastname;
}
public String getfirstname()
{
return firstname;
}
public String getlastname()
{
return lastname;
}
public String toString()
{
return firstname+"."+lastname;
}
}
public class TestIterator
{
public static void main(String[] args)
{
Collection c = new HashSet();
c.add(new Name("f1","l1"));
c.add(new Name("f2","l2"));
c.add(new Name("f3","l3"));
Iterator i = c.iterator();
while(i.hasNext())
{
Name n = (Name) i.next();//强制转换
System.out.println(n.getfirstname());
// System.out.println(i.next().getfirstname());//这样也能输出
}
}
}
* Iterator 接口的使用 所有的Collection接口类都有一个iterator方法
*以返回一个实现了Iterator接口的对象
*/
import java.util.*;
class Name
{
private String firstname;
private String lastname;
Name(String firstname,String lastname)
{
this.firstname = firstname;
this.lastname = lastname;
}
public String getfirstname()
{
return firstname;
}
public String getlastname()
{
return lastname;
}
public String toString()
{
return firstname+"."+lastname;
}
}
public class TestIterator
{
public static void main(String[] args)
{
Collection c = new HashSet();
c.add(new Name("f1","l1"));
c.add(new Name("f2","l2"));
c.add(new Name("f3","l3"));
Iterator i = c.iterator();
while(i.hasNext())
{
Name n = (Name) i.next();//强制转换
System.out.println(n.getfirstname());
// System.out.println(i.next().getfirstname());//这样也能输出
}
}
}