本帖最后由 凝聚 于 2013-10-6 11:30 编辑
import java.util.*;
public class six {
public static void main(String[] args) {
HashMap<Student,String>h=new HashMap<Student,String>();
h.put(new Student("orchestra",22),"shanghai");///////////////////////////////////
Set<Student>key=h.keySet();
Iterator<Student>it=key.iterator();
while(it.hasNext())
{
Student stu=it.next();
String addr=h.get(stu);
System.out.println(stu+"..."+addr);
}
}
class Student implements Comparable <Student>
{
private String name;
private int age;
Student(String name,int age)
{
this.age=age;
this.name=name;
}
public int compareTo (Student s)
{
int num=new Integer(this.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 getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+"..."+age;
}
}
}
这段代码中h.put(new Student("orchestra",22),"shanghai");这句语句有错误怎么回事再麻烦大家说一下 return name.hashCode()+age*34; 这句话中 age*34中*后的数字是什么意思
|