亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

各位大神,為什么我的map的contains方法弄不了啊?

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();

? ? }

}


正在回答

1 回答

查找學生姓名那里的?if(students.containsKey(new Student(null,namein)))

改成?if(students.containsValue(new Student(null,namein)))

就好啦!

望采納

0 回復 有任何疑惑可以回復我~
#1

慕粉0915566185 提問者

非常感謝!
2017-05-18 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

各位大神,為什么我的map的contains方法弄不了啊?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號