黑马程序员技术交流社区

标题: 联系TreeMap排序出现的错误 [打印本页]

作者: 王超    时间: 2012-6-16 16:29
标题: 联系TreeMap排序出现的错误
本帖最后由 王超 于 2012-6-16 21:38 编辑
  1. import java.util.*;
  2. class StuNameComparator implements Comparator<Student>
  3. {
  4.         public int compare(Student s1,Student s2)
  5.         {
  6.                 int num = s1.getName().compareTo(s2.getName());
  7.                 if(num==0)
  8.                 {
  9.                         return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  10.                 }
  11.                 return num;
  12.         }
  13. }

  14. class Student implements Comparable<Student>
  15. {
  16.         private String name;
  17.         private int age;
  18.         Student(String name, int age)
  19.         {
  20.                 this.name = name;
  21.                 this.age = age;
  22.         }

  23.         public int compareTo(Student s)
  24.         {
  25.                 int num = new Integer(this.age).compareTo(new Integer(s.age));
  26.                 if(num==0)
  27.                         return this.name.compareTo(s.name);
  28.                 return num;
  29.         }

  30.         public int hashCode()
  31.         {
  32.                 return name.hashCode()+age*34;
  33.         }
  34.         public boolean equals(Object obj)
  35.         {
  36.                 if(!(obj instanceof Student))
  37.                 {
  38.                         throw new ClassCastException("类型不匹配");
  39.                 }
  40.                 Student s = (Student)obj;
  41.                 return this.name.equals(s.name) && this.age==s.age;
  42.         }

  43.         public String setName(String name)
  44.         {
  45.                 return name;
  46.         }
  47.         public int setAge(int age)
  48.         {
  49.                 return age;
  50.         }
  51.         public void getName(String name)
  52.         {
  53.                 this.name = name;
  54.         }
  55.         public void getAge(int age)
  56.         {
  57.                 this.age = age;
  58.         }
  59.         public String toString()
  60.         {
  61.                 return name+":"+age;
  62.         }
  63. }



  64. class MapTest2
  65. {
  66.         public static void main(String[] args)
  67.         {
  68.                 TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());

  69.                 tm.put(new Student("blisi05",32),"beijing");
  70.                 tm.put(new Student("lisi06",52),"shanghai");
  71.                 tm.put(new Student("alisi02",49),"tianjin");
  72.                 tm.put(new Student("lisi01",25),"wuhan");

  73.                 Set<Map.Entry<Student,String>> entrySet = tm.entrySet();
  74.                 Iterator<Map.Entry<Student,String>> it = entrySet.iterator();

  75.                 while(it.hasNext())
  76.                 {
  77.                         Map.Entry<Student,String> me = it.next();
  78.                         Student stu = me.getKey();
  79.                         String addr = me.getValue();

  80.                         System.out.println(stu+"::"+addr);
  81.                 }
  82.         }
  83. }
复制代码
错误提示:
MapTest2.java:12: 无法将 Student 中的 getName(java.lang.String) 应用于 ()
                int num = s1.getName().compareTo(s2.getName());
                                                   ^
MapTest2.java:12: 无法将 Student 中的 getName(java.lang.String) 应用于 ()
                int num = s1.getName().compareTo(s2.getName());
                            ^
MapTest2.java:15: 无法将 Student 中的 getAge(int) 应用于 ()
                        return new Integer(s1.getAge()).compareTo(new Integer(s2
.getAge()));

^
MapTest2.java:15: 无法将 Student 中的 getAge(int) 应用于 ()
                        return new Integer(s1.getAge()).compareTo(new Integer(s2
.getAge()));
                                             ^
4 错误
作者: 李盼    时间: 2012-6-16 16:33
本帖最后由 李盼 于 2012-6-16 16:35 编辑


你的getName和setName写反了,换一下就可以了!

作者: 宋浩    时间: 2012-6-16 16:41
package Test;
import java.util.*;
class StuNameComparator implements Comparator<Student>
{
        public int compare(Student s1,Student s2)
        {
                int num = s1.getName().compareTo(s2.getName());
                if(num==0)
                {
                        return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                }
                return num;
        }
}
class Student implements Comparable<Student>
{
        private String name;
        private int age;
        Student(String name, int age)
        {
                this.name = name;
                this.age = 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 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 setName(String name)
        {
                return name;
        }
        public int setAge(int age)
        {
                return age;
        }
        public String getName()
        {
                return name;//这行代码有问题
        }
        public int getAge()
        {
                return age;
        }
        public String toString()
        {
                return name+":"+age;
        }
}

class MapTest2
{
        public static void main(String[] args)
        {
                TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());
                tm.put(new Student("blisi05",32),"beijing");
                tm.put(new Student("lisi06",52),"shanghai");
                tm.put(new Student("alisi02",49),"tianjin");
                tm.put(new Student("lisi01",25),"wuhan");
                Set<Map.Entry<Student,String>> entrySet = tm.entrySet();
                Iterator<Map.Entry<Student,String>> it = entrySet.iterator();
                while(it.hasNext())
                {
                        Map.Entry<Student,String> me = it.next();
                        Student stu = me.getKey();
                        String addr = me.getValue();
                        System.out.println(stu+"::"+addr);
                }
        }
}

你的getName()方法和getAge()方法应该是返回一个name和age的值,怎么写的跟set一样了,跟你改了一下,应该没有问题了!!





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