import java.util.hashmap怎么理解后面的路径
1个回答
展开全部
HASHMAP最好与实例联系起来..它主要存的是键与值的关系.
举个例子如你现在有一个学生类
import java.util.HashMap;
public class Student {
String name;
String sex;
public Student(String n,String s) {
name=n;
sex=s;
}
public String toString(){
return ("姓名:"+name+"\n"+"性别:"+sex+"\n");
}
public static void main(String [] args){
HashMap hm=new HashMap();
Student s1=new Student("张三","男");
Student s2=new Student("李四","男");
Student s3=new Student("小利","女");
//存值是根据学生编号存的学生信息
hm.put("001",s1);
hm.put("002",s2);
hm.put("003",s3);
//查找学生编号是001的学生
//因为hm.get("001")反回的是Object所以加上强转
Student s=(Student)hm.get("001");
System.out.println(s.toString());
//其余的类似
}
}
举个例子如你现在有一个学生类
import java.util.HashMap;
public class Student {
String name;
String sex;
public Student(String n,String s) {
name=n;
sex=s;
}
public String toString(){
return ("姓名:"+name+"\n"+"性别:"+sex+"\n");
}
public static void main(String [] args){
HashMap hm=new HashMap();
Student s1=new Student("张三","男");
Student s2=new Student("李四","男");
Student s3=new Student("小利","女");
//存值是根据学生编号存的学生信息
hm.put("001",s1);
hm.put("002",s2);
hm.put("003",s3);
//查找学生编号是001的学生
//因为hm.get("001")反回的是Object所以加上强转
Student s=(Student)hm.get("001");
System.out.println(s.toString());
//其余的类似
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询