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

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

為什么會出現(xiàn)這種結(jié)果?

http://img1.sycdn.imooc.com//556d106a00010cce02750381.jpg

package com.imooc.collection;

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


public class MapTest{

?? ?//用來承裝學生類型對象
?? ?public Map<String,Student> students;
?? ?
?? ?//在構(gòu)造器中初始化students屬性
?? ?public MapTest(){
?? ??? ?this.students=new HashMap<String,Student>();
?? ?}
?? ?
?? ?//測試添加:輸入學生ID,判斷是否被占用
?? ?//若未被占用,則輸入姓名,創(chuàng)建新學生對象,
?? ?//并且添加到students中,
?? ?
?? ?public void testPut(){
?? ??? ?//創(chuàng)建一個Scanner對象,用來獲取輸入的學生ID和姓名
?? ??? ?Scanner console=new Scanner(System.in);
?? ??? ?int i=0;
?? ??? ?while(i<3){
?? ??? ??? ?System.out.println("請輸入學生ID:");
?? ??? ??? ?String ID=console.next();
?? ??? ??? ?//判斷該ID是否被占用
?? ??? ??? ?Student st=students.get(ID);
?? ??? ??? ?if(st==null){
?? ??? ??? ??? ?//提示輸入學生姓名
?? ??? ??? ??? ?System.out.println("請輸入學生姓名:");
?? ??? ??? ??? ?String name=console.next();
?? ??? ??? ??? ?//創(chuàng)建新的學生對象
?? ??? ??? ??? ?Student newStudent=new Student(ID,name);
?? ??? ??? ??? ?//通過調(diào)用students的new方法,添加ID—學生映射
?? ??? ??? ??? ?students.put(ID, newStudent);
?? ??? ??? ??? ?System.out.println("成功添加學生:"+students.get(ID).name);
?? ??? ??? ??? ?i++;
?? ??? ??? ?}else{
?? ??? ??? ??? ?System.out.println("該學生ID已被占用!");
?? ??? ??? ??? ?continue;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?
?? ?//測試Map的keySet方法
?? ?public void testKeySet(){
?? ??? ?//通過keySet方法,返回Map中的所有“鍵”的Set集合
?? ??? ?Set<String> keySet=students.keySet();
?? ??? ?//取得students的容量
?? ??? ?System.out.println("總共有:"+students.size()+"個學生!");
?? ??? ?//遍歷keySet,取得每一個鍵,再調(diào)用get方法取得每個鍵對應(yīng)的value
?? ??? ?for (String stuId: keySet) {
?? ??? ??? ?Student st=students.get(stuId);
?? ??? ??? ?if(st!=null)
?? ??? ??? ??? ?System.out.println("學生:"+st.name);
?? ??? ?}
?? ??? ?}
?? ?//測試刪除Map中的映射
?? ?public void testRemove(){
?? ??? ?//獲取從鍵盤輸入的待刪除學生ID字符串
?? ??? ??? ?Scanner console=new Scanner(System.in);
?? ??? ??? ?while(true){
?? ??? ??? ??? ?//提示輸入待刪除的學生的ID
?? ??? ??? ??? ?System.out.println("請輸入要刪除的學生ID!");
?? ??? ??? ??? ?String ID=console.next();
?? ??? ??? ??? ?//判斷該ID 是否有對應(yīng)的學生對象
?? ??? ??? ??? ?Student st=students.get(ID);
?? ??? ??? ??? ?if(st==null){
?? ??? ??? ??? ??? ?//提示輸入的ID并不存在
?? ??? ??? ??? ??? ?System.out.println("該ID不存在!");
?? ??? ??? ??? ??? ?continue;
?? ??? ??? ??? ????? }
?? ??? ??? ??? ?students.remove(ID);
?? ??? ??? ??? ?System.out.println("成功刪除學生:"+st.name);
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ?}
?? ?
?? ?//通過entrySet方法來遍歷Map
?? ?public void testEntrySet(){
?? ??? ?//通過entrySet方法,返回Map中的所有鍵值對
?? ??? ?Set<Entry<String,Student>> entrySet=students.entrySet();
?? ???? for(Entry<String,Student>entry:entrySet){
?? ??? ?System.out.println("取得鍵:"+entry.getKey());
?? ??? ?System.out.println("對應(yīng)的值為:"+entry.getValue().name);
?? ?}
?? ?}
?? ?
public static void main(String[] args) {
?? ?MapTest mt=new MapTest();
?? ?mt.testPut();
?? ?mt.testKeySet();
?? ?mt.testRemove();
?? ?mt.testEntrySet();
?? ?
?? ?
}
}

正在回答

1 回答

testPut()沒有出錯,問題可能出在Student類里,我寫了一個Student類,然后,復(fù)制了你那個testPut()方法,結(jié)果運行正常。

public?class?Student?{
????String?id;
????String?name;
????public?Student(){}
????public?Student(String?id,?String?name)?{
????????this.id?=?id;
????????this.name?=?name;
????}
}


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

L小小Q 提問者

非常感謝!
2015-06-03 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

為什么會出現(xiàn)這種結(jié)果?

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

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

幫助反饋 APP下載

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

公眾號

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