}
class Student implements Comparable<Student>
{
private String name;
private int age;
public Student(String name,int age)
{
this.name=name;
this.age=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 new Integer(this.name.length()).compareTo(new Integer(s.name.length()));//名字按字符串长度来判断
return num;
}
}
发生了这两个错误The method getKey() is undefined for the type TreeMap<Student,String>
The method getValue() is undefined for the type TreeMap<Student,String> 作者: ithmC4 时间: 2014-5-28 10:38
public static void main(String args[])
{
TreeMap<Student,String> tm=new TreeMap<Student,String>();
tm.put(new Student("lisi",20),"beijing");
tm.put(new Student("zhangsan",19),"nanjing");
Set<Map.Entry<Student,String>> entryset=tm.entrySet();
Iterator<Map.Entry<Student,String>> it=entryset.iterator();
while(it.hasNext())
{
Map.Entry<Student,String> ms=it.next();
////////////////////////////////////////////////////////////////////
Student s= ms.getKey();//①用ms②Entry的取出方法和TreeMap的取出方法不同的
String add=ms.getValue();//③你现在要搞明白为什么用Entry来迭代Map,感觉你还不明白这个
////////////////////////////////////////////////////////////////////
System.out.println(s.getName()+"::"+s.getAge());
}
}作者: pk49800 时间: 2014-5-28 14:26