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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 毛标 中级黑马   /  2012-8-8 10:41  /  1277 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 毛标 于 2012-8-8 15:55 编辑

import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name,int age)
{
  this.name=name;
  this.age=age;
}
public int hashCode()
{
  return name.hashCode()+age*34;
}
public boolean equals(Object obj)
{
  if(!(obj instanceof Student))
   throw new ClassCastException("类型不匹配");
   Student s=(Student)obj;
  return this.name.equals(s.name) && this.age==s.age;
}
public String getName()
{
  return name;
}
public int getAge()
{
  return age;
}
public int compareTo(Student s)
{
  int num= new Integer(this.age).compareTo(new Integer(s.age));
  if(num==0)
   return this.name.compareTO(s.name);
  return num;
}
public String toString()
{
  return name+":"+age;
}
}
class MapTest
{
public static void main(String[] args)
{
  HashMap<Student,String> hm=new HashMap<Student,String>();
  hm.put(new Student("lisi1",21),"beijing");
   hm.put(new Student("lisi2",22),"shanghai");
   hm.put(new Student("lisi3",23),"xian");
   hm.put(new Student("lisi4",24),"nanjing");
   Set<Student> keySet=hm.keySet();
  Iterator<Student> it=keySet.Iterator();
  while(it.hasNext())
  {
   Student stu=it.next();
   String addr=hm.get(stu);
   System.out.println(stu+":"+addr);
  }
}
} 求指教



未命名.jpg (29.6 KB, 下载次数: 11)

未命名.jpg

评分

参与人数 1技术分 +1 收起 理由
田建 + 1 加油!!

查看全部评分

4 个回复

倒序浏览
  1. import java.util.*;
  2. class Student implements Comparable<Student>
  3. {
  4. private String name;
  5. private int age;
  6. Student(String name,int age)
  7. {
  8.   this.name=name;
  9.   this.age=age;
  10. }
  11. public int hashCode()
  12. {
  13.   return name.hashCode()+age*34;
  14. }
  15. public boolean equals(Object obj)
  16. {
  17.   if(!(obj instanceof Student))
  18.    throw new ClassCastException("类型不匹配");
  19.    Student s=(Student)obj;
  20.   return this.name.equals(s.name) && this.age==s.age;
  21. }
  22. public String getName()
  23. {
  24.   return name;
  25. }
  26. public int getAge()
  27. {
  28.   return age;
  29. }
  30. public int compareTo(Student s)
  31. {
  32.   int num= new Integer(this.age).compareTo(new Integer(s.age));
  33.   if(num==0)
  34.    return this.name.compareTo(s.name);//这里注意,应该是compareTo
  35.   return num;
  36. }
  37. public String toString()
  38. {
  39.   return name+":"+age;
  40. }
  41. }
  42. class MapTest
  43. {
  44. public static void main(String[] args)
  45. {
  46.   HashMap<Student,String> hm=new HashMap<Student,String>();
  47.   hm.put(new Student("lisi1",21),"beijing");
  48.    hm.put(new Student("lisi2",22),"shanghai");
  49.    hm.put(new Student("lisi3",23),"xian");
  50.    hm.put(new Student("lisi4",24),"nanjing");
  51.    Set<Student> keySet=hm.keySet();
  52.   Iterator<Student> it=keySet.iterator();//iterator开头是小写
  53.   while(it.hasNext())
  54.   {
  55.    Student stu=it.next();
  56.    String addr=hm.get(stu);
  57.    System.out.println(stu+":"+addr);
  58.   }
  59. }
  60. }
复制代码
都是些小问题,以后仔细一点
回复 使用道具 举报
问题已解决
回复 使用道具 举报
郑小杰 发表于 2012-8-8 11:03
都是些小问题,以后仔细一点

粗心了~~以后一定注意
回复 使用道具 举报
问题已解决
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马