学生管理类 容器

package student;

public class student {

private String num;
private String name;
public student(String num,String name){
this.num = num;
this.name = name;
}

@Override
public String toString() {
return "student [num=" + num + ", name=" + name + "]";
}

public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

package student;

import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;

public class test {
private student S1 = new student("1","zhang");
private student S2 = new student("2","li");
private student S3 = new student("3","wang");
Map<String,student> map =
new HashMap<String, student>();
public test(){
map.put("1",S1);
map.put("2",S2);
map.put("3",S3);
}
public String getStr(){
//System.out.print("请输入:");
Scanner in = new Scanner(System.in);
String Str = in.next();
return Str;
}
public void keyFind(){
System.out.println("请输入键值查询:");
String key = getStr();
boolean contains = map.containsKey(key);

if(contains){
System.out.println("存在该学生 "+key+map.get(key).toString());
}
else{
System.out.println("不存在该学生 "+key);
}

}

public void Add(){
System.out.print("请输入键值进行添加:");
String key = getStr();
System.out.print("请输入学号:");
String num = getStr();
System.out.print("请输入姓名:");
String name = getStr();
student S = new student(num,name);
map.put(key, S);
}
public void getSize(){
System.out.println("学生个数:"+map.size());

}
public void remove(){
System.out.println("输入键值进行删除");
String key = getStr();
map.remove(key);
}
public static void main(String[] args) {
test t = new test();

t.keyFind();
t.Add();
t.getSize();
t.remove();
t.getSize();

}

}

原文地址:https://www.cnblogs.com/the-wang/p/6628797.html