import java.util.*;
class Student{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public String toStrint(){
return name+":::"+age;
}
}
class xuexiao{
/*
public void xuexiao1(){
TreeMap<String,ArrayList<Student>> xuexiao=new TreeMap<String,ArrayList<Student>>();
ArrayList<Student> yiban=new ArrayList<Student>();
ArrayList<Student> erban=new ArrayList<Student>();
xuexiao.put("yiban",yiban);
xuexiao.put("erban",erban);
yiban.add(new Student("zhangsan",12));
yiban.add(new Student("zhangsan",13));
yiban.add(new Student("zhangsan",14));
erban.add(new Student("wangwu",23));
erban.add(new Student("wangwu",24));
erban.add(new Student("wangwu",22));
这一段不能放在这个类里的方法里,虽然你在main里调用了该方法但是当这方法执行完这段代码就从内存里消失了,
所以造成了你的困惑,把这段代码放到main里执行就行了
}*/
public void getStudent(TreeMap<String,ArrayList<Student>> xuexiao,ArrayList<Student> banji){
Iterator<String> it=xuexiao.keySet().iterator();
while(it.hasNext()){
String s=it.next();
ArrayList<Student> ss=xuexiao.get(s);
if(ss==banji)
System.out.println(s);//还有你这里面的代码有问题,打印的是内存引用 你修改下就好了,我都不帮你改了
Iterator<Student> itr=banji.iterator();
while(itr.hasNext()){
Student sss=itr.next();
if(ss==banji)
System.out.println(sss);
}
}
}
}
class txt01
{
public static void main(String[] args)
{
TreeMap<String,ArrayList<Student>> xuexiao=new TreeMap<String,ArrayList<Student>>();
ArrayList<Student> yiban=new ArrayList<Student>();
ArrayList<Student> erban=new ArrayList<Student>();
xuexiao.put("yiban",yiban);
xuexiao.put("erban",erban);
yiban.add(new Student("zhangsan",12));
yiban.add(new Student("zhangsan",13));
yiban.add(new Student("zhangsan",14));
erban.add(new Student("wangwu",23));
erban.add(new Student("wangwu",24));
erban.add(new Student("wangwu",22));
xuexiao xx=new xuexiao();
xx.getStudent(xuexiao,erban);
}//我就是想这么传,但是这是错误的参数,运行就Error,上面的代码我应该怎么写,这里可以这么传?
} |