A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 盛亚昆 中级黑马   /  2012-3-18 19:06  /  2974 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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;//做了两次强转啊,Person p=(Person)it.next();不是强转了一回了吗????
                System.out.println(this.name+"...."+p.name);
                return this.name.equals(p.name)&&this.age==p.age;//equals和pubLic boolean equals(Object obj)怎么不是一个啊
               
        }
        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("wangwu01",30));
                al.add(new Person("wangwu01",30));
                al.add(new Person("wangwu03",33));
                al.add(new Person("wangwu04",34));
                al.add(new Person("wangwu04",34));
                al =singleElement(al);

               
                Iterator it=al.iterator();
                while (it.hasNext())
                {
                       
                        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();
                        if (!newAl.contains(obj))
                        {
                                newAl.add(obj);
                        }
                }
       
                return newAl;
        }

}

评分

参与人数 1技术分 +1 收起 理由
房宝彬 + 1

查看全部评分

8 个回复

倒序浏览
如果要是进行年龄属性进行排序那 要怎么运用啊
回复 使用道具 举报
ArrayList没有这个功能 要用TreeSet集合 覆盖compareTo方法  
回复 使用道具 举报
if (!(obj instanceof Person))//是判断你传进来的obj是否是Person的一个实例对象,
如果不是p.name和p.age也就没意义了,而且Person p=(Person)obj也会出现类转换异常。
两次强转是因为它们在不同的方法中,只有满足条件,调用那个方法才有意义。

评分

参与人数 1技术分 +1 收起 理由
房宝彬 + 1

查看全部评分

回复 使用道具 举报
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;//做了两次强转啊,Person p=(Person)it.next();不是强转了一回了吗????                System.out.println(this.name+"...."+p.name);                这里强转是因为Person类要覆盖Object类中的equals方法,你看这个方法的参数类型都是Object的  而你return 中要
                                                      比较的name 和age 都是子类中(就是Person)的成员属性啊  这也是多态的体现 必须向下转型  要不会出现类型转换异常的
                return this.name.equals(p.name)&&this.age==p.age;//equals和pubLic boolean equals(Object obj)怎么不是一个啊
               
        }
        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("wangwu01",30));
                al.add(new Person("wangwu01",30));
                al.add(new Person("wangwu03",33));
                al.add(new Person("wangwu04",34));
                al.add(new Person("wangwu04",34));
                al =singleElement(al);

               
                Iterator it=al.iterator();
                while (it.hasNext())
                {
                        
                        Person p=(Person)it.next();//                         这里强转是因为it.next() 是去除集合中存放对象的内存地址,相当于对象的引用 ,而这些引用是  
                  sop(p.getName()+"%%%%"+p.getAge());    怎么添加进去的? 是add(Object obj);添加进去的,  相当于
                                                                                         add(Object obj = newnew Person("wangwu01",30));    这是多态,而你调用的是子类对象中的方法
)                                                                                    多以必须向下转型了啊
                }

        }
        public static ArrayList singleElement(ArrayList al)
        {
               
                ArrayList newAl=new ArrayList();
                Iterator it=al.iterator();
                while (it.hasNext())
                {
                        Object obj=it.next();
                        if (!newAl.contains(obj))
                        {
                                newAl.add(obj);
                        }
                }
        
                return newAl;
        }

}
回复 使用道具 举报
package one;

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))//这里是什么意思啊????//这句话的意思是: 判断具体对象是否是指定的类型。格式是:对象 instanceof 类型
                        return false;

                Person p=(Person)obj;//做了两次强转啊,Person p=(Person)it.next();不是强转了一回了吗????//都是强转了一回呀,把具体的对象,按指定的类型,进行强转的
                               
                System.out.println(this.name+"...."+p.name);
                return this.name.equals(p.name)&&this.age==p.age;//equals和pubLic boolean equals(Object obj)怎么不是一个啊//前面一个是Object中的提供的公共方法,后面的一个是自定义的方法名
               
        }
        public String getName()
        {
                return name;
        }
        public int getAge()
        {
        
                return age;
        }
}

public 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("wangwu01",30));
                al.add(new Person("wangwu01",30));
                al.add(new Person("wangwu03",33));
                al.add(new Person("wangwu04",34));
                al.add(new Person("wangwu04",34));
                al =singleElement(al);

               
                Iterator it=al.iterator();
                while (it.hasNext())
                {
                        
                        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();
                        if (!newAl.contains(obj))
                        {
                                newAl.add(obj);
                        }
                }
        
                return newAl;
        }

}

回复 使用道具 举报
明白了 谢谢指点
回复 使用道具 举报
李飞 中级黑马 2012-3-19 00:12:05
8#
instanceof是判断传进来的obj是否是Person类型的
如果不是肯定就会出现强制转换异常,就像如果将猫强制转换成狗,是不可能的,猫不会汪汪的叫呀

写程序的时候要确保程序的健壮性
回复 使用道具 举报
贠(yun)靖 发表于 2012-3-18 23:13
import java.util.*;
class Person
{

Iterator it=al.iterator();//Arraylist类实现了迭代器这个接口所以他可以调用迭代方法获取迭代器
while (it.hasNext()){//如果迭代器还可以往后移
Person p=(Person)it.next();// 那么依次返回迭代器对应的元素,因为ArrayList没有限定元素类型所以必须强转
sop(p.getName()+"%%%%"+p.getAge());    }    //可以指定泛型 就可以不需要强转 即  ArrayList<person>    和     Iterator<person>      
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马