不会在主函数中调用equal方法,怎么调用,还有这是一个去掉重复元素的程序,但是现在不知道如何调用,所以去不掉重复的元素.- import java.util.*;
- class ArrayListDemo2
- {
- private String name;
- private int age;
- ArrayListDemo2(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 ArrayListDemo2)
- {
- return false;
- }
- ArrayListDemo2 a = (ArrayListDemo2)obj;
- return this.name.equals(a.name) && this.age == a.age;
-
- }
- public static ArrayList SingleElement(ArrayList al)
- {
- ArrayList a = new ArrayList();
- Iterator it = al.iterator();
- while(it.hasNext())
- {
- Object obj = it.next();
- if(!a.contains(obj))
- a.add(obj);
- }
- return a;
- }
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- public static void main(String[] args)
- {
- ArrayList al = new ArrayList();
- al.add(new ArrayListDemo2("zhao",20));
- al.add(new ArrayListDemo2("zhao",20));
- al.add(new ArrayListDemo2("qian",20));
- al.add(new ArrayListDemo2("zhao",20));
- al.add(new ArrayListDemo2("sun",20));
- al.add(new ArrayListDemo2("zhao",20));
- al=SingleElement(al);
- Iterator it = al.iterator();
- while(it.hasNext())
- {
- ArrayListDemo2 al2 = (ArrayListDemo2)it.next();
- sop(al2.getName()+";;"+al2.getAge());
- }
- }
- }
复制代码
|
|