package lian;
import java.util.*;
//c创建狗狗的类
class Dog{
private String name;
private int age;
Dog(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Dog))
return false;
Dog d=(Dog)obj;
return this.name.equals(d.name)&&age==d.age;
}
}
public class ListDome {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建多个狗狗的对象
Dog d=new Dog("ww",1);
Dog d1=new Dog("qw",5);
Dog d2=new Dog("ew",3);
Dog d3=new Dog("rw",2);
Dog d4=new Dog("tw",1);
//创建集合
ArrayList<Dog> ar=new ArrayList<Dog>();
ar.add(d);
ar.add(d1);
ar.add(d2);
ar.add(d3);
ar.add(d4);
//创建迭代器
for(ListIterator<Dog> i=ar.listIterator();i.hasNext();)
{
Dog g=(Dog)i.next();
System.out.println(g.getName()+" "+g.getAge());
}
//移除用remove
ar.remove(2);
ar.remove(new Dog("rw",2));
//遍历
for(Iterator<Dog> i=ar.iterator();i.hasNext();)
{
Dog g=(Dog)i.next();
System.out.println(g.getName()+"------"+g.getAge());
}
System.out.println(ar.contains(new Dog("rw",1)));
}
}
|
|