public static void main(String[] args){
student stud[]= new student[2];
stud[0]=new student("wanglei",1);
stud[1]=new student("zhangping",2);
LinkedList mylist=new LinkedList();
mylist.add(stud[0]);
mylist.add(stud[1]);
//Hashtable hash=new Hashtable();
/*System.out.println("this list has " + mylist.size()+ "element");
Iterator iter=mylist.iterator();
while(iter.hasNext()){
student te=(student)iter.next();
System.out.println(te.name);
System.out.println(te.id);
}*/
System.out.println("input your name");
String str=new BufferedReader(new InputStreamReader(System.in)).readLine();
try{
str=new BufferedReader(new InputStreamReader(System.in)).readLine();
}
catch(IOException nef){
System.out.println(nef.getMessage());
}
if (mylist.contains(str)){            <---------------这里
System.out.println("你要查找的的信息" + str.name);
System.out.println(str.id);
}
else {
System.out.println("没有你要查找的信息");
}
我把数组对象存入了mylist里,每个对象数组有各自的属性(姓名.id),为了响应用户输入的数组的属性(姓名),来查找该数组的其他信息。 我用contains方法,可是他的参数需要是对象类型(在我这里是数组对象),怎么才能根据用户输入的名称 找到他所对应的对象,然后再访问其他信息?

解决方案 »

  1.   

    用Hashtable 把姓名与 对象对应  挺麻烦,有别的方法吗
      

  2.   

    好像你的程序根本就编译通不过啊..
    public static void main(String[] args){
    student stud[]= new student[2];
    stud[0]=new student("wanglei",1);   //这里再new一次好像有问题
    stud[1]=new student("zhangping",2);
    LinkedList mylist=new LinkedList();
    mylist.add(stud[0]);
    mylist.add(stud[1]);
    //Hashtable hash=new Hashtable();
    /*System.out.println("this list has " + mylist.size()+ "element");
    Iterator iter=mylist.iterator();
    while(iter.hasNext()){
    student te=(student)iter.next();
    System.out.println(te.name);
    System.out.println(te.id);
    }*/
    System.out.println("input your name");
    String str=new BufferedReader(new InputStreamReader(System.in)).readLine();  //下面又new了一次??
    try{
    str=new BufferedReader(new InputStreamReader(System.in)).readLine();
    }
    catch(IOException nef){
    System.out.println(nef.getMessage());
    }
    if (mylist.contains(str)){ //str你定义为String类型,怎么有name和id两个属性了呢?你的意思应该是student类型的吧!
    System.out.println("你要查找的的信息" + str.name);
    System.out.println(str.id);
    }
    else {
    System.out.println("没有你要查找的信息");
    }
    至于不能查找String类型,你可以这样:
    if( mylist.contains(new student(name,id)) ){  };  name和id自己输入的.
     好像是不能根据一个属性来查找的吧,参数应该要与你add的参数相同吧!
    全是个人理解,不知道是否有错?
      

  3.   

    不明白的是 为什么要用对象数组呢?LinkedList list = new LinkedList(15);
    Student stu = new Student("s1",1);
    list.add(stu);
    stu = new Student("s2",2);
    list.add(stu);
    就OK的嘛