<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ~~~ package com.imooc.collection; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set; public class MapTest { /** * 用來承裝學生類型對象 */ public Map<String, Student> students; /** * 初始化students屬性 */ public MapTest() { this.students = new HashMap<String, Student>(); } /** * 測試添加:輸入學生ID,判斷是否被占用 * 若未被占用,則輸入姓名,創建新學生對象 * 并且添加到students中 */ public void testPut() { //創建一個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(); //創建新的學生對象 Student newStudent = new Student(ID, name); //通過調用Students的put方法,添加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,取得每一個鍵,再調用get方法取得每一個鍵對應的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) { System.out.println("請輸入要刪除的學生ID!"); String ID = console.next(); 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<Map.Entry<String, Student>> entrySet = students.entrySet(); for (Map.Entry<String, Student> entry : entrySet) { System.out.println("取得鍵:" + entry.getKey()); System.out.println("對應的值為:" + entry.getValue().name); } } /** * 利用put方法修改Map中的已有映射 */ public void testModify() { //提示輸入要修改的學生ID System.out.println("請輸入要修改的學生ID:"); //創建一個Scanner對象,去獲取從鍵盤上輸入的學生ID字符串 Scanner console = new Scanner(System.in); while (true) { //取得從鍵盤輸入的學生ID String stuID = console.next(); //查找該學生ID是否存在 Student student = students.get(stuID); if (student == null) { System.out.println("該ID不存在!請重新輸入!"); continue; } //提示當前對應的學生對象的姓名 System.out.println("當前該ID所對應的學生為:" + student.name); //提示輸入新的學生姓名來修改已有的映謝 System.out.println("請輸入新的學生姓名:"); String name = console.next(); Student newStudent = new Student(stuID, name); students.put(stuID,newStudent); System.out.println("修改成功!"); break; } } public static void main(String[] args) { MapTest mt = new MapTest(); mt.testPut(); mt.testKeySet(); //mt.testRemove(); //mt.testEntrySet(); mt.testModify(); mt.testEntrySet(); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看