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方法,可是他的参数需要是对象类型(在我这里是数组对象),怎么才能根据用户输入的名称 找到他所对应的对象,然后再访问其他信息?
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方法,可是他的参数需要是对象类型(在我这里是数组对象),怎么才能根据用户输入的名称 找到他所对应的对象,然后再访问其他信息?
解决方案 »
- java中字符比较大小的问题
- 请教一个关于 stack 和 queue class 的问题
- 一简单的不能再简单的问题!
- 我用的是:jdk-1_5_0_04,可报错:软件包 javax.servlet 不存在?在线等待!
- 安装了java3d-1_3_1-windows-i586-opengl-sdk.exe 不知道如何配置!
- 求解~
- Java反射问题invoke方法疑惑
- main 裡面 的(String a[]) 與 (String[] a) 有什麼區別
- 调用HTML
- javascript的函数如何在HTML中使用求大神指教
- 求解正则表达式,匹配用户输入的内容是符合预定类型中哪个类型.
- 有没方法把字符的ip转换成数字型 用于ip限制功能
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的参数相同吧!
全是个人理解,不知道是否有错?
Student stu = new Student("s1",1);
list.add(stu);
stu = new Student("s2",2);
list.add(stu);
就OK的嘛