在学习集合迭代时,突然想到这个问题,怎么让迭代后,原集合不变,或者把原集合复制一下?
public static void main(String[] args)
{
ArrayList a = new ArrayList();
a.add(new Person("Ri1",21));
a.add(new Person("Ri2",22));
a.add(new Person("Ri3",23));
a.add(new Person("Ri1",21));
a.add(new Person("Ri2",22));
a.add(new Person("Ri3",23));
Iterator it = a.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
sop(p.getName()+"::"+p.getAge());
}
//先迭代打印后,怎么让a中元素不变,再执行下面的去除重复操作,打印出来,对比看一下去重操作?
a=quChong(a); //去除重复对象方法
Iterator it = al.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
sop(p.getName()+"::"+p.getAge());
}
}