為什么輸出的是key值?哪位大佬告訴我哈
public class MapTest {
public Map<String, Student> students;
public MapTest() {
this.students = new HashMap();
}
public void mapAdd() {
Scanner console = new Scanner(System.in);
int i = 0;
while (i < 3) {
System.out.println("請(qǐng)輸入學(xué)生ID:");
String ID = console.next();
Student stu = students.get(ID);
if (stu == null) {
System.out.println("input student's name:");
String name = console.next();
Student newStudent = new Student(ID, name);
students.put(ID, newStudent);
System.out.println("成功添加:" + students.get(ID).name);
i++;
} else {
System.out.println("已經(jīng)存在ID再輸:");
continue;
}
}
testKeySet();
}
public void testKeySet() {
Set<String> keySet = students.keySet();
for (String stuId : keySet) {
Student stu = students.get(stuId);
if (stu != null) {
System.out.print(stu.name);
}
}
}
public static void main(String[] args) {
MapTest mt = new MapTest();
mt.mapAdd();
}
}
請(qǐng)輸入學(xué)生ID:
1
input student's name:
tom
成功添加:1
請(qǐng)輸入學(xué)生ID:
2
input student's name:
jay
成功添加:2
請(qǐng)輸入學(xué)生ID:
3
input student's name:
jack
成功添加:3
123
2018-01-15
解決了~