黑马程序员技术交流社区
标题:
联系TreeMap排序出现的错误
[打印本页]
作者:
王超
时间:
2012-6-16 16:29
标题:
联系TreeMap排序出现的错误
本帖最后由 王超 于 2012-6-16 21:38 编辑
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 void getName(String name)
{
this.name = name;
}
public void getAge(int age)
{
this.age = 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);
}
}
}
复制代码
错误提示:
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 编辑
public void getName(String name)
{
this.name = name;
}
你的getName没有返回值啊,怎么能调用compareTo方法呢?
你的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