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

© 黄长利 中级黑马   /  2012-4-2 17:49  /  1775 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;
  2. class MapTest2
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 TreeMap<Student,Address> tm = new TreeMap<Student,Address>();
  7.                 tm.put(new Student("蒙奇·D·路飞",19), new Address("东海"));
  8.                 tm.put(new Student("爱德华·纽盖特",72), new Address("新世界3"));
  9.                 tm.put(new Student("乔拉可尔·密佛格",43), new Address("新世界2"));
  10.                 tm.put(new Student("马尔高",32), new Address("新世界1"));
  11.                 tm.put(new Student("索隆",23), new Address("东海"));

  12.                 Set<Map.Entry<Student, Address>> entrySet = tm.entrySet();
  13.                 Iterator<Map.Entry<Student, Address>> it = entrySet.iterator();
  14.                 while(it.hasNext())
  15.                 {
  16.                         Map.Entry<Student, Address> me = it.next();
  17.                         Student key = me.getKey();
  18.                         Address value = me.getValue();
  19.                         System.out.println(key.getName()+".."+key.getAge()+".."+value.getAddress());
  20.                 }
  21.         }
  22. }

  23. class Student implements Comparable<Student>//实现Comparable,复写其compareTo方法。
  24. {
  25.         private String name;
  26.         private int age;
  27.         Student(String name, int age)
  28.         {
  29.                 this.name = name;
  30.                 this.age = age;
  31.         }
  32.         public int compareTo(Student s)
  33.         {
  34.                 int num = this.name.compareTo(s.name);
  35.                 if(num==0)
  36.                 {
  37.                         return new Integer(this.age).compareTo(new Integer(s.name));
  38.                 }
  39.                 return num;
  40.         }
  41.         public int hashCode() //复写哈希表数据结构的 hashCode方法
  42.         {
  43.                 return this.name.hashCode()+age*21;
  44.         }
  45.         public boolean equals(Object obj) //复写哈希表数据结构的 equals 方法。
  46.         {
  47.                 if(!(obj instanceof Student))
  48.                         throw new ClassCastException("对象类型不匹配");
  49.                 Student s = (Student)obj;
  50.                 return this.name.equals(s.name) && this.age==s.age;
  51.         }
  52.         public String getName()
  53.         {
  54.                 return name;
  55.         }
  56.         public int getAge()
  57.         {
  58.                 return age;
  59.         }
  60.         public String toString()
  61.         {
  62.                 return name+":"+age;
  63.         }
  64. }

  65. class Address
  66. {
  67.         private String address;
  68.         Address(String address)
  69.         {
  70.                 this.address = address;
  71.         }
  72.         public String getAddress()
  73.         {
  74.                 return address;
  75.         }
  76. }
复制代码
以上代码,在编译之后出现了异常,看了许久也没找到问题在哪,求解。。编译结果如下图:

3 个回复

倒序浏览
public int compareTo(Student s)

35.        {

36.                int num = this.name.compareTo(s.name);

37.                if(num==0)

38.                {

39.                        return new Integer(this.age).compareTo(new Integer(s.name));//这个地方出错了
40.                }

41.                return num;

42.        }

return new Integer(this.age).compareTo(new Integer(s.name));这行代码有问题,new Integer(s.name),s.name是字符串,应改为new Integer(s.age)
回复 使用道具 举报
第39行代码出错
回复 使用道具 举报
39行代码写错了,
return new Integer(this.age).compareTo(new Integer(s.name));

改为return new Integer(this.age).compareTo(new Integer(s.age));

new Integer(s.name)这句代表着把字符串s.name转为一个Integer对象,但s.name 不是可解析的整数,所以会发生NumberFormatException异常

当你试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,就会抛出该异常
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马