第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

同樣的問題,為什么我寫的equals和hashcode方法還是返回false?

同樣的問題,為什么我寫的equals和hashcode方法還是返回false?

正在回答

4 回答

http://img1.sycdn.imooc.com//59a2999300014eec10900490.jpg

錯誤如上圖。

1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

左岸的風(fēng)有咖啡香

是的是的。這里沒有接觸過的很容易搞錯。我就被坑在這好久
2017-11-13 回復(fù) 有任何疑惑可以回復(fù)我~

public class Maptest {

Scanner input = new Scanner(System.in);

//private static final String ID = null;

private Map<String,Student> Students;

public Maptest(){

this.Students=new HashMap<String,Student>();

}

public void testPut(){

Scanner input=new Scanner(System.in);

int i=1;

while(i<=3){//3個學(xué)生

System.out.print("請輸入第"+i+"學(xué)生ID:");

String ID=input.next();

Student st = Students.get(ID);//保存id,判斷id是否被占用

if(st==null){//如果當(dāng)前id為空

System.out.print("請輸入學(xué)生姓名:");

String name=input.next();

Student stu=new Student();//保存學(xué)生信息,set保存信息

stu.set(ID,name);

Students.put(ID,stu);//通過put,添加id學(xué)生映射,保存id?

System.out.println("添加學(xué)生:"+Students.get(ID).name);

i++;//i加1

}else{//否則

System.out.println("id已存在");

continue;//繼續(xù)循環(huán)

}

}

}

public void testKeySet(){ //打印出所有學(xué)生

Set<String>keySet=Students.keySet();//通過KeySet方法,返回Map中的所有"鍵"的Set集合

//取得Students容量

System.out.println("總共有"+Students.size()+"名學(xué)生");//size返回

for(String stuid:keySet){//學(xué)生id

? ? ? ? ? Student st=Students.get(stuid);

? ? ? ? ? if(st!=null)//st如果不為空

? ? ? ? ? System.out.println("學(xué)生:"+st.id+":"+st.name);

? ? ? ? ? }

?}

//刪除學(xué)生

public void romoveStu(){

Scanner input=new Scanner(System.in);

System.out.println("請輸入要刪除學(xué)生的數(shù)量:");

int a=input.nextInt();

while(true){

for(int i=1;i<=a;i++){

if(a>Students.size()){

System.out.println("數(shù)量大于學(xué)生總量!?。?);

continue;

}else if(a==0){

break;

}else{

}

System.out.print("請輸入要刪除的第"+i+"位學(xué)生ID:");

String ID=input.next();

Student st=(Student)Students.get(ID);

? ? ? ? ? ? ? ? ? if(st==null){?

System.out.println("id不存在");

continue;

}

Students.remove(ID);

System.out.println("成功刪除學(xué)生:"+st.name);

}

break;

}

}

//通過entrySet方法來遍歷Map

public void testentrySet(){//輸出被刪除后剩余的學(xué)生

Set<Entry<String,Student>>entrySet=Students.entrySet();

for (Entry<String, Student> entry : entrySet) {

System.out.println("剩余學(xué)生id:"+entry.getKey());//對應(yīng)的id

System.out.println("學(xué)生姓名:"+entry.getValue().name);//對應(yīng)的值

}


}

//修改學(xué)生內(nèi)容

public void testModifly(){

System.out.println("請輸入要修改的學(xué)生id:");

Scanner input = new Scanner(System.in);

while(true){

String stuId = input.next();

Student st=Students.get(stuId);//保存當(dāng)前id

if(st==null){

System.out.println("id不存在");

continue;

}else{

System.out.println("當(dāng)前ID所對應(yīng)的學(xué)生:"+st.name);

System.out.println("請輸入新的學(xué)生姓名:");

String name=input.next();

String id;

Student newStu=new Student();//重新保存信息

newStu.set(stuId,name);

Students.put(stuId,newStu);//屬性.put(信息變量名,對象名)

System.out.println("修改成功!:");

System.out.println("修改后的學(xué)生:");

testKeySet();

break;

}


}

}

//測試map中是否包含某個key值或value值

public void testContainsKeyOrVaule(){

System.out.print("請輸入要查詢的學(xué)生id:");

String id=input.next();

System.out.println("您輸入的學(xué)生:"+id+","+"是否存在:"+Students.containsKey(id));

if(Students.containsKey(id)){

? ? System.out.println("學(xué)生姓名:"+Students.get(id).name); //輸出和id對應(yīng)的姓名

}

}

//containsValue方法判斷學(xué)生是否存在

public void testContainsKeyOrVaule1(){

System.out.println("請輸入學(xué)生姓名:");

String name=input.next();

if(Students.containsValue(name)){

System.out.println("包含學(xué)生"+name+"對應(yīng)的id"+Students.get(name).id);

}else{

System.out.println("不包含");

}

}

public static void main(String[] args) {

Maptest p=new Maptest();

p.testPut();

p.testKeySet();

p.romoveStu();

p.testentrySet();

p.testKeySet();//刪除學(xué)生后在調(diào)用所有學(xué)生的方法是刪除學(xué)生后的學(xué)生

//p.testModifly();//修改學(xué)生

p.testContainsKeyOrVaule();

p.testContainsKeyOrVaule1();

}

}


0 回復(fù) 有任何疑惑可以回復(fù)我~

public class Student {

String id;//學(xué)生

String name;

static Set <Class>cla;//學(xué)生所選的課程信息屬性

public ?Student(String id,String name){//將最終的值賦給構(gòu)造方法

this.name = name;

this.id=id;

this.cla=new HashSet<Class>();

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((id == null) ? 0 : id.hashCode());

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (!(obj instanceof Student))

return false;

Student other = (Student) obj;

if (id == null) {

if (other.id != null)

return false;

} else if (!id.equals(other.id))

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

return true;

}

}


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕標(biāo)6149872

就這些咋看,主程序那?
2017-04-21 回復(fù) 有任何疑惑可以回復(fù)我~

首先,你得把代碼貼出來

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

東方的小王 提問者

看樓下
2017-04-19 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

同樣的問題,為什么我寫的equals和hashcode方法還是返回false?

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號