package student;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Maptest {
public Map<String,STUDENT> Students;
public Maptest(){
this.Students=new HashMap<String,STUDENT>();
}
public void Testput(){
Scanner console=new Scanner(System.in);
int i=0;
while(i<3){
System.out.println("请输入学生ID:");
int ID=console.nextInt();
STUDENT ST=Students.get(ID);
if(ST==null){
System.out.println("请输入学生姓名:");
String name=console.next();
STUDENT newStudent=new STUDENT(ID,name);
Students.put("ID", newStudent);
System.out.println("成功添加学生:"+Students.get(ID).getName());
i++;
}
else{
System.out.println("该ID已被占用!");
continue;
}
}
}
public void Testkeyset(){
Set<String> Keyset=Students.keySet();
System.out.println("总共有:"+Keyset.size());
for(String stuID:Keyset){
STUDENT st=Students.get(stuID);
if(st!=null){
System.out.println("学生:"+st.getName());
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Maptest mp=new Maptest();
mp.Testput();
mp.Testkeyset();
}
}
// 创建新的学生对象
Student newStudent = new Student(ID, name);
// 通过调用students的put方法,添加ID-学生映射
students.put(ID, newStudent);
为什么新建的学生对象里面已经有了ID和name 两个参数了。在put方法里面还要添加ID这个属性?
个人感觉得你的问不是很大,你仔细看看 其实你在没有添加对象的时候就开始从student的对象中取元素了!你再看看TestPut方法中这句STUDENT ST=Students.get(ID)的位置!
package student;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Maptest {
public Map<String,STUDENT> Students;
public Maptest(){
this.Students=new HashMap<String,STUDENT>();
}
public void Testput(){
Scanner console=new Scanner(System.in);
int i=0;
while(i<3){
System.out.println("请输入学生ID:");
String ID=console.next();
STUDENT ST=Students.get(ID);
if(ST==null){
System.out.println("请输入学生姓名:");
String name=console.next();
STUDENT newStudent=new STUDENT(ID,name);
Students.put(ID, newStudent);
System.out.println("成功添加学生:"+Students.get(ID).getName());
i++;
}
else{
System.out.println("该ID已被占用!");
continue;
}
}
}
public void Testkeyset(){
Set<String> Keyset=Students.keySet();
System.out.println("总共有:"+Keyset.size());
for(String stuID:Keyset){
STUDENT st=Students.get(stuID);
if(st!=null){
System.out.println("学生:"+st.getName());
}
}
}
public static void main(String[] args) {
Maptest mp=new Maptest();
mp.Testput();
mp.Testkeyset();
}
}
package student;
import java.util.HashSet;
import java.util.Set;
public class STUDENT {
private String id;
private String name;
public static Set<CLASS> Class;
public String getId(){
return id;
}
public void setId(String id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public STUDENT(String id,String name){
this.id=id;
this.name=name;
this.Class=new HashSet<CLASS>();
}
}
STUDENT类
解决了,,在STUDENT类里的Class加了一个static就解决了,,虽然不知为什么得加static
System.out.println("成功添加学生:"+Students.get(ID).getName());
请输入学生ID:
1
请输入学生姓名:
小明
Exception in thread "main" java.lang.NullPointerException
at student.Maptest.Testput(Maptest.java:25)
at student.Maptest.main(Maptest.java:49)