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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 范龙波 高级黑马   /  2013-5-8 05:26  /  1801 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 范龙波 于 2013-5-8 21:06 编辑

import java.util.*;



class  StudentDemo
{
        public static void main(String[] args)
        {
                HashMap<String,List<Student>> CZBK=new HashMap<String,List<Student>>();
                List<Student> YuReBan=new ArrayList<Student>();
                List<Student> JiuYeBan=new ArrayList<Student>();
                CZBK.put("第一层",YuReBan);
                CZBK.put("第二层",JiuYeBan);
                YuReBan.add(new Student("lisi01",26));
                YuReBan.add(new Student("lisi02",29));
                YuReBan.add(new Student("lisi03",26));
                YuReBan.add(new Student("lisi04",29));
                JiuYeBan.add(new Student("李四02",28));
                JiuYeBan.add(new Student("李四01",29));
                sop(YuReBan);
                sop(JiuYeBan);
                sop(CZBK);
                GetMap(YuReBan);
                Iterator<String> iter=CZBK.keySet().iterator();
                while(iter.hasNext())
                {
                        String n=iter.next();
                        List<Student> XueSheng=CZBK.get(n);
                        sop(n);
                        GetMap(XueSheng);  //打印出来的是学生的地址值不是里面的具体内容非常郁闷,帮忙找一下问题出在哪里了,谢谢。
        
                }
               
               



        }
        public static void sop(Object obj)
        {
                System.out.println(obj);
        }
        public static void GetMap(List<Student> tm) //我总感觉问题好像出在了这里,可是没有找到。
        {
                Iterator<Student> it=tm.iterator();
                while(it.hasNext())
                {
                        Student s=it.next();
                        sop(s);
                }
        }
        

}
class Student implements Comparable<Student>
{
        private String name;
        private int age;
        public void SetName(String name)
        {
                this.name=name;
        }
        public String GetName()
        {
                return name;
        }
        public void SetAge(int age)
        {
                this.age=age;
        }
        public int GetAge()
        {
                return age;
        }
        public int compareTo(Student stu)
        {
                int in=new Integer(this.age).compareTo(new Integer((stu.age)));
                if(in==0)
                        this.name.compareTo(stu.name);
                return in;
        }
        public int hashCode()
        {
                return  name.hashCode()+age*5;
        }
        public boolean equals(Object obj)
        {
                if(!(obj instanceof Student))
                        throw new ClassCastException("传递的对象不是学生");
                Student st=(Student)obj;
                return this.name.equals(st.name)&&this.age==st.age;
        }
        public Student(String name,int age)
        {
                this.name=name;
                this.age=age;
        }

}

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

3 个回复

倒序浏览
  1. import java.util.*;


  2. class  StudentDemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 HashMap<String,List<Student>> CZBK=new HashMap<String,List<Student>>();
  7.                 List<Student> YuReBan=new ArrayList<Student>();
  8.                 List<Student> JiuYeBan=new ArrayList<Student>();
  9.                 CZBK.put("第一层",YuReBan);
  10.                 CZBK.put("第二层",JiuYeBan);
  11.                 YuReBan.add(new Student("lisi01",26));
  12.                 YuReBan.add(new Student("lisi02",29));
  13.                 YuReBan.add(new Student("lisi03",26));
  14.                 YuReBan.add(new Student("lisi04",29));
  15.                 JiuYeBan.add(new Student("李四02",28));
  16.                 JiuYeBan.add(new Student("李四01",29));
  17. //                sop(YuReBan);
  18. //                sop(JiuYeBan);
  19. //                sop(CZBK);
  20. //                GetMap(YuReBan);
  21.                 Iterator<String> iter=CZBK.keySet().iterator();
  22.                 while(iter.hasNext())
  23.                 {
  24.                     String n=iter.next();
  25.                     List<Student> XueSheng=CZBK.get(n);
  26.                     sop(n);
  27.                     GetMap(XueSheng);  //打印出来的是学生的地址值不是里面的具体内容非常郁闷,帮忙找一下问题出在哪里了,谢谢。
  28.         
  29.                 }

  30.         }
  31.         public static void sop(Object obj)
  32.         {
  33.                 System.out.println(obj);
  34.         }
  35.         public static void GetMap(List<Student> tm) //我总感觉问题好像出在了这里,可是没有找到。
  36.         {
  37.                 Iterator<Student> it=tm.iterator();
  38.                 while(it.hasNext())
  39.                 {
  40.                         Student s=it.next();
  41.                         sop(s);
  42.                        
  43.                         //方法二:将sop(s)改成 sop(s.GetName()+".."+s.GetAge());//调用学生的具体方法来返回学生对象的内容
  44.                 }
  45.         }
  46.         

  47. }
  48. class Student implements Comparable<Student>
  49. {
  50.         private String name;
  51.         private int age;
  52.         public void SetName(String name)
  53.         {
  54.                 this.name=name;
  55.         }
  56.         public String GetName()
  57.         {
  58.                 return name;
  59.         }
  60.         public void SetAge(int age)
  61.         {
  62.                 this.age=age;
  63.         }
  64.         public int GetAge()
  65.         {
  66.                 return age;
  67.         }
  68.         public int compareTo(Student stu)
  69.         {
  70.                 int in=new Integer(this.age).compareTo(new Integer((stu.age)));
  71.                 if(in==0)
  72.                         this.name.compareTo(stu.name);
  73.                 return in;
  74.         }
  75.         public int hashCode()
  76.         {
  77.                 return  name.hashCode()+age*5;
  78.         }
  79.         public boolean equals(Object obj)
  80.         {
  81.                 if(!(obj instanceof Student))
  82.                         throw new ClassCastException("传递的对象不是学生");
  83.                 Student st=(Student)obj;
  84.                 return this.name.equals(st.name)&&this.age==st.age;
  85.         }
  86.         public Student(String name,int age)
  87.         {
  88.                 this.name=name;
  89.                 this.age=age;
  90.         }
  91.       //这是解决办法之一:让学生对象覆盖toString方法,因为输出对象是输出toString方法中返回的东西,
  92.       //如果不重写 输出的 默认格式是: 对象名@哈希值。
  93.         public String toString()  
  94.                                                        
  95.         {
  96.                 return name+"..."+age;
  97.         }

  98. }
复制代码

评分

参与人数 2技术分 +1 黑马币 +3 收起 理由
黄玉昆 + 1
范龙波 + 3 很给力!谢谢

查看全部评分

回复 使用道具 举报
刘学明    发表于 2013-5-8 10:00

明白了非常感谢。
回复 使用道具 举报
如果问题未解决,请继续追问,如果问题解决了,请将分类改为“已解决”,谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马