import java.util.*;
class Person
{
private String name;
private int age;
Person(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 Person))
return false;
Person p = (Person)obj;
return this.name.equals(p.getName()) && this.age == p.age;
}
}
class Test
{
public static void main(String[] args)
{
ArrayList list = new ArrayList();
list.add(new Person("java01", 1));
list.add(new Person("java02", 2));
list.add(new Person("java02", 3));
list.add(new Person("java03", 3));
list.add(new Person("java04", 4));
list.add(new Person("java05", 5));
sop(list);
sop(signalElement(list));
}
public static ArrayList signalElement(ArrayList list)
{
ArrayList ar = new ArrayList();
Iterator it = list.iterator();
while(it.hasNext())
{
Object obj = it.next();
if(!ar.contains(obj))
ar.add(obj);
}
return ar;
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
编译结果为什么会是
[Person@1b8d6f7, Person@290fbc, Person@1c80b01, Person@4aa0ce, Person@1833eca, P
erson@18f5824]
[Person@1b8d6f7, Person@290fbc, Person@1c80b01, Person@4aa0ce, Person@1833eca, P
erson@18f5824]
求好心人解答!!
|
|