本帖最后由 范龙波 于 2013-5-8 21:06 编辑
import java.util.*;
class StudentDemo
{
public static void main(String[] args)
{
HashMap<String,List<Student>> CZBK=new HashMap<String,List<Student>>();
List<Student> YuReBan=new ArrayList<Student>();
List<Student> JiuYeBan=new ArrayList<Student>();
CZBK.put("第一层",YuReBan);
CZBK.put("第二层",JiuYeBan);
YuReBan.add(new Student("lisi01",26));
YuReBan.add(new Student("lisi02",29));
YuReBan.add(new Student("lisi03",26));
YuReBan.add(new Student("lisi04",29));
JiuYeBan.add(new Student("李四02",28));
JiuYeBan.add(new Student("李四01",29));
sop(YuReBan);
sop(JiuYeBan);
sop(CZBK);
GetMap(YuReBan);
Iterator<String> iter=CZBK.keySet().iterator();
while(iter.hasNext())
{
String n=iter.next();
List<Student> XueSheng=CZBK.get(n);
sop(n);
GetMap(XueSheng); //打印出来的是学生的地址值不是里面的具体内容非常郁闷,帮忙找一下问题出在哪里了,谢谢。
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void GetMap(List<Student> tm) //我总感觉问题好像出在了这里,可是没有找到。
{
Iterator<Student> it=tm.iterator();
while(it.hasNext())
{
Student s=it.next();
sop(s);
}
}
}
class Student implements Comparable<Student>
{
private String name;
private int age;
public void SetName(String name)
{
this.name=name;
}
public String GetName()
{
return name;
}
public void SetAge(int age)
{
this.age=age;
}
public int GetAge()
{
return age;
}
public int compareTo(Student stu)
{
int in=new Integer(this.age).compareTo(new Integer((stu.age)));
if(in==0)
this.name.compareTo(stu.name);
return in;
}
public int hashCode()
{
return name.hashCode()+age*5;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("传递的对象不是学生");
Student st=(Student)obj;
return this.name.equals(st.name)&&this.age==st.age;
}
public Student(String name,int age)
{
this.name=name;
this.age=age;
}
}
|