慕粉0915566185
2017-05-07 00:10
package listarrray;
public class Student {
String id;
String name;
Courses selectcourses;
public Student(String id,String name){
this.id=id;
this.name=name;
}
public Student(){
}
@Override
public boolean equals(Object obj){
if(this==obj){
return true;
}else{
if(obj==null){
return false;
}else{
if(!(obj instanceof Student)){
return false;
}else{
Student std=(Student)obj;
if(this.name==null){
if(std.name==null){
return true;
}else{
return false;
}
}else{
if(this.name.equals(std.name)){
return true;
}else{
return false;
}
}
}
}
}
}
@Override
public int hashCode() {
final int t=31;
int hash = 1;
hash = t*hash+((name==null)?0:name.hashCode());
return hash;
}
}
package listarrray;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
Map<String,Student> students;
public MapTest(){
students=new HashMap<String,Student>();
int i=0;
while(i<3){
System.out.print("请输入学生的ID:");
Scanner in=new Scanner(System.in);
String ID=in.next();
Student st=students.get(ID);
if(st==null){
System.out.print("请输入学生姓名:");
String newstudent=in.next();
Student NewStudent=new Student(ID,newstudent);
NewStudent.name=newstudent;
students.put(ID, NewStudent);
System.out.println("添加学生"+NewStudent.name+"成功!");
i++;
}else{
System.out.println("该ID已被占用!");
continue;
}
}
}
public void containstest(){
System.out.print("请输入想要查找的学生的key:");
Scanner in=new Scanner(System.in);
String keyin=in.next();
if(students.containsKey(keyin)){
System.out.println("Yes,Someone here!his name is: "+students.get(keyin).name);
}else{
System.out.println("Sorry,No one here!");
}
System.out.print("请输入想要查找的学生的name:");
String namein=in.next();
if(students.containsKey(new Student(null,namein))){
System.out.println("Yes,Someone here!his name is: "+namein);
}else{
System.out.println("Sorry,No one here!");
}
}
public void getMap(){
// keyset方法是将map中的Key集合转化为set集合;
Set<String> studentid=students.keySet();
System.out.println("一共有"+students.size()+"个学生");
for (String sss:studentid) {
Student st1=students.get(sss);
if(st1!=null){
System.out.println("学生:"+students.get(sss).name);
}
}
}
public void removemap(){
Scanner in1=new Scanner(System.in);
while(true){
System.out.print("请输入要删除的学生的ID:");
String removeID=in1.next();
Student removest=students.get(removeID);
if(removest!=null){
students.remove(removeID);
System.out.println("成功删除学生"+removest.name);
break;
}else{
System.out.println("该学生不存在!");
}
}
}
public void entrytest(){
//系统的entry有属于自己的泛型,需要把它改成对应map集合的泛型;entryset方法是将map转化为set集合的方法;
Set<Entry<String, Student>>entryset=students.entrySet();
System.out.println("所有学生:");
for(Entry<String,Student> entry:entryset){
//entry的set集合元素可调用getKey()和getValue()方法分别取得map元素的Key和Value;
System.out.println("第"+entry.getKey()+"学生:"+entry.getValue().name);
}
}
public void changemap(){
Scanner in2=new Scanner(System.in);
while(true){
System.out.print("请输入要修改的学生的ID:");
String changeid=in2.next();
Student changest=students.get(changeid);
if(changest==null){
System.out.println("输入的ID不存在,请重新输入!");
continue;
}
System.out.println("该学生目前的名字为:"+changest.name);
System.out.print("请输入修改的名字:");
String changename=in2.next();
Student newstudent1=new Student(changeid,changename);
students.put(changeid, newstudent1);
System.out.println("修改成功!");
break;
}
}
public static void main(String[] args) {
MapTest ttt=new MapTest();
ttt.getMap();
ttt.containstest();
// ttt.removemap();
// ttt.entrytest();
// ttt.changemap();
// ttt.entrytest();
}
}
查找学生姓名那里的 if(students.containsKey(new Student(null,namein)))
改成 if(students.containsValue(new Student(null,namein)))
就好啦!
望采纳
Java入门第三季
409792 学习 · 4340 问题
相似问题