黑马程序员技术交流社区

标题: 帮忙帮我看一下这个异常是怎么抛出的;自己看很久了没... [打印本页]

作者: 曾林魁    时间: 2013-6-4 22:32
标题: 帮忙帮我看一下这个异常是怎么抛出的;自己看很久了没...

  1. package TreeSet;
  2. import java.util.*;
  3. public class TreeSetEntrySet
  4. {
  5. public static void main(String[] args)
  6. {
  7. HashMap<Student,String> ma = new HashMap<Student,String>();
  8. //ma.put(new Student("zeng01",21),"beijing");
  9. ma.put(new Student("zeng01",21),"beijing");
  10. ma.put(new Student("zeng02",24),"wuhan");
  11. ma.put(new Student("zeng04",22),"zhengzhuo");
  12. ma.put(new Student("zeng03",23),"changshang");


  13. /*Set<Student> s = ma.keySet();

  14. for(Student stu:s)
  15. {
  16. String name= stu.getName();
  17. int age = stu.getAge();
  18. String value = ma.get(stu);
  19. sop(name+"...."+age+"..."+value);
  20. }*/
  21. Set<Map.Entry<Student, String>> entry = ma.entrySet();

  22. for(Iterator<Map.Entry<Student,String>> it = entry.iterator();it.hasNext();)
  23. {
  24. Map.Entry<Student,String> en = it.next();
  25. Student stu =en.getKey();
  26. String addr = en.getValue();
  27. sop(stu.getName()+"..."+stu.getAge()+"....."+addr);
  28. }




  29. }

  30. static void sop(Object obj)
  31. {
  32. System.out.println(obj);
  33. }
  34. }
  35. class Student implements Comparable<Student>
  36. {
  37. private String name;
  38. private int age;
  39. public Student(String name,int age)
  40. {
  41. this.name = name;
  42. this.age=age;
  43. }
  44. public String getName()
  45. {
  46. return this.name;
  47. }
  48. public int getAge()
  49. {
  50. return this.age;
  51. }
  52. public int compareTo(Student stu)
  53. {
  54. int num = age-stu.getAge();
  55. if(num==0)
  56. return name.compareTo(stu.getName());
  57. return num;
  58. }
  59. public int hashCode()
  60. {
  61. return name.hashCode()+age*17;
  62. }
  63. public boolean equals(Object obj)
  64. {
  65. if(obj instanceof Student)
  66. throw new RuntimeException("类型不同!!");
  67. Student stu = (Student)obj;
  68. return name.equals(stu.getName())&& age==stu.getAge();
  69. }
  70. }
复制代码
当存入重复键值对时运行时抛出如下异常:Exception in thread "main" java.lang.RuntimeException: 类型不同!!
at TreeSet.Student.equals(TreeSetEntrySet.java:76)
at java.util.HashMap.put(HashMap.java:475)
at TreeSet.TreeSetEntrySet.main(TreeSetEntrySet.java:10)
他是怎么抛出的
大家帮忙看看  谢谢了

作者: 神之梦    时间: 2013-6-4 23:21
本帖最后由 神之梦 于 2013-6-4 23:25 编辑

if(obj instanceof Student)

throw new RuntimeException("类型不同!!");

这个if里面要加!
应该这样:
if(!(obj instanceof Student))
     throw new RuntimeException("类型不同!!");

作者: yin1031468524    时间: 2013-6-5 00:27
  1. package com.itheima;

  2. import java.util.*;

  3. public class TreeSetEntrySet
  4. {
  5.         public static void main(String[] args)
  6.         {
  7.                 HashMap<Student, String> ma = new HashMap<Student, String>();
  8.                 ma.put(new Student("zeng01", 21), "beijing");
  9.                 ma.put(new Student("zeng01", 21), "beijing");
  10.                 ma.put(new Student("zeng02", 24), "wuhan");
  11.                 ma.put(new Student("zeng04", 22), "zhengzhuo");
  12.                 ma.put(new Student("zeng03", 23), "changshang");

  13.                 /*
  14.                  * Set<Student> s = ma.keySet();
  15.                  *
  16.                  * for(Student stu:s) { String name= stu.getName(); int age =
  17.                  * stu.getAge(); String value = ma.get(stu);
  18.                  * sop(name+"...."+age+"..."+value); }
  19.                  */
  20.                 Set<Map.Entry<Student, String>> entry = ma.entrySet();

  21.                 for (Iterator<Map.Entry<Student, String>> it = entry.iterator(); it
  22.                                 .hasNext();)
  23.                 {
  24.                         Map.Entry<Student, String> en = it.next();
  25.                         Student stu = en.getKey();
  26.                         String addr = en.getValue();
  27.                         sop(stu.getName() + "..." + stu.getAge() + "....." + addr);
  28.                 }

  29.         }

  30.         static void sop(Object obj)
  31.         {
  32.                 System.out.println(obj);
  33.         }
  34. }

  35. class Student implements Comparable<Student>
  36. {
  37.         private String name;
  38.         private int age;

  39.         public Student(String name, int age)
  40.         {
  41.                 this.name = name;
  42.                 this.age = age;
  43.         }

  44.         public String getName()
  45.         {
  46.                 return this.name;
  47.         }

  48.         public int getAge()
  49.         {
  50.                 return this.age;
  51.         }

  52.         public int compareTo(Student stu)
  53.         {
  54.                 int num = age - stu.getAge();
  55.                 if (num == 0)
  56.                         return name.compareTo(stu.getName());
  57.                 return num;
  58.         }

  59.         public int hashCode()
  60.         {
  61.                 return name.hashCode() + age * 17;
  62.         }

  63.         public boolean equals(Object obj)
  64.         {
  65.                 if (!(obj instanceof Student)) // 此处应该是!(obj instanceof
  66.                                                                                 // Student),不然在equals时比较的obj instanceof
  67.                                                                                 // Student则会抛出这个异常
  68.                         throw new RuntimeException("类型不同!!");
  69.                 Student stu = (Student) obj;
  70.                 return name.equals(stu.getName()) && age == stu.getAge();
  71.         }
  72. }
复制代码
见注释部分、、、




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