- import java.util.*;
- class Person
- {
- private String name;
- private int age;
- Person(String name,int age)
- {
- this.name=name;
- this.age=age;
- }
-
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Person))
- return false;
- Person p=(Person)obj;
-
- return this.name.equals(p.name)&&this.age==p.age;
- }
-
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- }
- class ArrayListTest2
- {
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
-
- public static void main(String[] args)
- {
- ArrayList al=new ArrayList();
-
- al.add(new Person("lisi01",30));//al.add(Object obj); Object obj=new Person("lisi01",30);Person会被提升为Object类型。
- al.add(new Person("lisi02",32));
- al.add(new Person("lisi02",32));
- al.add(new Person("lisi03",33));
- al.add(new Person("lisi04",35));
- al.add(new Person("lisi04",35));
-
- al=singleElement(al);
-
- Iterator it=al.iterator();
-
- while(it.hasNext())
- {
- /*
- Object obj=it.next();
- Person p=(Person)obj;
- */
-
- Person p=(Person)it.next();
- sop(p.getName()+":::"+p.getAge());
- }
- }
-
-
- public static ArrayList singleElement(ArrayList al)
- {
- //定义一个临时容器
- ArrayList newAl=new ArrayList();
-
- Iterator it= al.iterator();//遍历老集合
-
- while(it.hasNext())
- {
- Object obj=it.next();//获取一个元素用obj记下来
-
- if(!newAl.contains(obj))
- newAl.add(obj);
- }
- return newAl;
- }
- }
复制代码
return this.name.equals(p.name)&&this.age==p.age; 大家好 这句话哪位大神帮小弟解答下啊 没整懂 this和p分别指的是什么啊 |