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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Map中的key值是自己設定的嗎

Map中的key值是自己設定的嗎

飛行烤鴨 2017-01-10 12:36:23
難道這里的key值是Map中存放的Student的id屬性??Student類代碼如下:/**?* 學生類?* @author yang?*2017年1月9日?*/public class Student {?public String id;?public String name;?public Set<Course>courses;?public Student(String id,String name){??this.id=id;??this.name=name;??this.courses=new HashSet<Course>();?}}public MapTest(){??students=new HashMap<String,Student>();?}??/**? * 測試添加:鍵入學生ID,判斷是否被占用,? * 若未被占用,則輸入姓名,創建新學生對象,并且添加到students中? * @param args? */?public void testPut(){??Scanner console=new Scanner(System.in);??int i=0;??while(i<3){???System.out.println("請輸入學生ID");???String ID=console.next();???Student st=students.get(ID);???if(st==null){???//提示輸入學生姓名????System.out.println("請輸入學生姓名");????String name=console.next();????Student newStudent=new Student(ID, name);????students.put(ID, newStudent);????System.out.println("成功添加學生"+students.get(ID).name+students.get(ID).id);????i++;???}???else{????System.out.println("該學生ID已被占用");????continue;???}??}?}??/**? * 測試Map的keySet方法? * @param args? */?public void testKeySet(){??//通過keySet方法,返回Map中的所有“鍵”的Set集合??//Map的keySet方法,返回的是Map中所有"鍵"的Set集合,Set集合喲!??!??Set<String> keySet=students.keySet();??//遍歷KeySet,取得每一個鍵,再調用get方法取得每個鍵對應的value。??System.out.println("總共有"+students.size()+"個學生");??for(String stuId:keySet){???Student st=students.get(stuId);???if(st!=null){???System.out.println("學生"+st.name);???}??}?}
查看完整描述

2 回答

已采納
?
快樂的時光

TA貢獻79條經驗 獲得超48個贊

Map的key和value依據你的需求你自己設定的,例如上面的代碼?students=new HashMap<String,Student>();

你限定key為學生學號,為String類型,value為student實體類,為Student類型.

主要是依據你的需求自己設定的。

查看完整回答
1 反對 回復 2017-01-10
?
路過轉

TA貢獻4條經驗 獲得超10個贊

map中key和value的數據類型是寫代碼的人自己設定的,然后根據設定好的數據類型來輸入數據

查看完整回答
3 反對 回復 2017-01-10
  • 2 回答
  • 0 關注
  • 2689 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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