黑马程序员技术交流社区

标题: 程序中的错误求解【已解决】 [打印本页]

作者: 毛标    时间: 2012-8-8 10:41
标题: 程序中的错误求解【已解决】
本帖最后由 毛标 于 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, 下载次数: 41)

未命名.jpg

作者: 郑小杰    时间: 2012-8-8 11:03
  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:45
问题已解决
作者: 毛标    时间: 2012-8-8 11:46
郑小杰 发表于 2012-8-8 11:03
都是些小问题,以后仔细一点

粗心了~~以后一定注意
作者: 毛标    时间: 2012-8-8 15:51
问题已解决




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2