<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 集合框架 > 在集合框架中,主要分為 Collection 和 Map 兩大接口。 > 在 Collection 中又分為 List (有序集合) 和 Set (無序集合)。List 和 Set 都是接口。 > Map 存儲的 Key-Value 結構。 > 集合框架存儲的元素可以是不定長的,即可以任意的添加,但是數組在定義的時候就確定了長度。實際開發中,對于同類型數據的結構,一般使用 Collection 結構會較多。 ## Collection ### List > 是實際開發中用的最多的一種數據結構,存儲的單個元素,使用**泛型**去強制約束 List 中存放的是一致的數據類型 **插入元素** add() **遍歷元素** - 通過元素索引下標方式 for 循環 - 通過 foreach 循環 - 通過迭代器 Iterator **刪除元素** 如果通過遍歷去查找相關元素進行刪除的時候,不要使用 list.remove(int index) 該方法,因為使用該方法,會動態的直接改變集合的元素結構,導致遍歷的不完整或者有錯誤。要使用迭代器遍歷集合,調用 Iterator.remove() 方法刪除。 ### Set 不詳細闡述,在遍歷的時候不能使用索引方式,只能通過迭代器和 foreach。 ## Map 類似的可以理解給集合元素中的值定義了一個 Key 鍵(遍歷),之后可以通過相關的方法快速的定位到具體的 Value 中。 **獲取某個 key 的 value** Value get(Key) **如何遍歷 Map 結構** 獲取 Map 的 Key 集合,通過遍歷 Key 集合,獲取 Value 值。 1. 獲取 key 集合:map.keySet(); 2. 遍歷 Key 集合:iterator; 3. 獲取 Value 值:get(key); ## 開發注意事項 在實際開發中,List 一般使用 ArrayList 實現類去構建,Map 一般使用 HashMap 實現類去構建。 要掌握對于 List 和 Map 的添加元素、遍歷元素。 要認識集合框架中關于對象在內存中的存儲方法。 ## 使用 Collecionts 工具類 **使用 Collections.sort(list) 進行排序** 1. 集合中的元素類要實現 Comparable 接口,重寫 compareTo 方法; 2. 調用 Collections.sort(list) 完成排序 ## 本節課的代碼片段 **代碼運行內存圖** ![](https://box.kancloud.cn/31f0032935ff547f4e79ad3975896039_1979x849.png) Clazz.java ~~~ package com.ntqingniao.j96.bean; import java.util.List; public class Clazz { private String name; private String code; private List<Student> students; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } } ~~~ Clazz2.java ~~~ package com.ntqingniao.j96.bean; import java.util.Map; public class Clazz2 { private String name; private String code; private Map<String, Student> stus; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public Map<String, Student> getStus() { return stus; } public void setStus(Map<String, Student> stus) { this.stus = stus; } } ~~~ Client.java ~~~ package com.ntqingniao.j96.bean; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Client { public static void main(String[] args) { Student pxj = new Student("彭賢君", "001"); Student hjn = new Student("黃佳男", "002"); Clazz j96 = new Clazz(); j96.setName("Java96班"); j96.setCode("java96"); // Step1:學生類型的集合定義完成了 List<Student> stus = new ArrayList<Student>(); // Step2: 添加數據 stus.add(pxj); stus.add(hjn); // Step3: 追加一個新的學生 Student ljn = new Student("陸佳楠", "003"); stus.add(ljn); // Step4: 設置班級學生屬性 j96.setStudents(stus); // for (int i = 0; i < j96.getStudents().size(); i++) { // Student stu = j96.getStudents().get(i); // if (stu.getName().equals("黃佳男")) { // stu.setCode("004"); // } // } // for (int i = 0; i < j96.getStudents().size(); i++) { // // 打印對象,如果沒有顯式的調用toString()方法,編譯器也會執行toString(); // System.out.println(j96.getStudents().get(i)); // } // // for (int i = 0; i < stus.size(); i++) { // System.out.println(stus.get(i)); // } // // for (Student stu : stus) { // System.out.println(stu); // } // // for (Student stu : stus) { // if (stu.getName().equals("黃佳男")) { // stu.setCode("005"); // } // } // for (int i = 0; i < j96.getStudents().size(); i++) { // Student stu = j96.getStudents().get(i); // System.out.println(stu.getName()); // if (stu.getName().equals("彭賢君")) { // j96.getStudents().remove(i); // 這樣的實現是有問題的 // System.out.println(j96.getStudents().size()); // } // } // // for (Student stu : stus) { // System.out.println(stu); // } // 使用迭代器遍歷 // Step1:創建迭代器 Iterator<Student> it = stus.iterator(); while (it.hasNext()) { Student stu = it.next(); System.out.println(stu); if (stu.getName().equals("黃佳男")) { it.remove(); } } System.out.println("處理完的結果======================"); it = stus.iterator(); while (it.hasNext()) { Student stu = it.next(); System.out.println(stu); } } } ~~~ Client2.java ~~~ package com.ntqingniao.j96.bean; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Client2 { public static void main(String[] args) { Student pxj = new Student("彭賢君", "001"); Student hjn = new Student("黃佳男", "002"); Clazz2 j96 = new Clazz2(); j96.setName("Java96班"); j96.setCode("java96"); Map<String, Student> stus = new HashMap<String, Student>(); stus.put("001", pxj); stus.put("002", hjn); j96.setStus(stus); Student stu = stus.get("001"); System.out.println(stu.getName()); System.out.println(stus.containsKey("003")); // 遍歷Map // 第一步:獲取Map中的Key集合 Set<String> keySet = stus.keySet(); // 第二步:迭代Key集合 Iterator<String> keyIt = keySet.iterator(); while (keyIt.hasNext()) { String key = keyIt.next(); // 根據Key獲取Value Student value = stus.get(key); System.out.println(key + "=" + value); } } } ~~~ Client3.java ~~~ package com.ntqingniao.j96.bean; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Client3 { public static void main(String[] args) { Student pxj = new Student("彭賢君", "10", 20); Student hjn = new Student("黃佳男", "008",19); Student liuxiaowei = new Student("劉曉衛", "002",22); Student liuxiaowei2 = new Student("劉曉衛", "005",15); Student liuxiaowei3 = new Student("劉曉衛", "001",25); Student liuxiaowei4 = new Student("劉曉衛", "004",13); List<Student> stus = new ArrayList<Student>(); stus.add(pxj); stus.add(hjn); stus.add(liuxiaowei); stus.add(liuxiaowei2); stus.add(liuxiaowei3); stus.add(liuxiaowei4); Collections.sort(stus); for(Student stu : stus) { System.out.println(stu); } } } ~~~ Student.java ~~~ package com.ntqingniao.j96.bean; public class Student implements Comparable<Student>{ private String name; private String code; private int age; public Student(String name, String code) { super(); this.name = name; this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(String name, String code, int age) { super(); this.name = name; this.code = code; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", code=" + code + ", age=" + age + "]"; } @Override public int compareTo(Student o) { if (this.age > o.age) { return 1; } else if (this.age == o.age) { return 0; } else { return -1; } } } ~~~ **撲克牌實例程序** > 由于出現花色特殊的字符,需要把Java文件的字符編碼定義為 UTF-8 Constants.java ~~~ package com.ntqingniao.j96.bean; public class Constants { public static String[] FOLLOWS = {"?","?","?","?"}; public static int[] NUMBERS = {2,3,4,5,6,7,8,9,10,11,12,13,14}; public static String[] NUMBERS_STR = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; } ~~~ Poker.java ~~~ package com.ntqingniao.j96.bean; public class Poker { private String follow; private int number; public int compare(Poker p1, Poker p2) { return 0; } public Poker(String follow, int number) { super(); this.follow = follow; this.number = number; } public String getFollow() { return follow; } public void setFollow(String follow) { this.follow = follow; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public String toString() { return this.getFollow() + Constants.NUMBERS_STR[this.getNumber()-2]; } } ~~~ Client4.java ~~~ package com.ntqingniao.j96.bean; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Client4 { public static void main(String[] args) { List<Poker> pokers = new ArrayList<Poker>(); for (String follow : Constants.FOLLOWS) { for (int number : Constants.NUMBERS) { Poker poker = new Poker(follow, number); pokers.add(poker); } } int i = 0; for (Poker poker : pokers) { System.out.print(poker + " "); i++; if (i % 13 == 0) { System.out.println(); } } System.out.println("==========洗牌=========="); for (int j = 0; j < 5; j++) { System.out.println("===========第" + (j + 1) + "副牌=========="); Collections.shuffle(pokers); i = 0; for (Poker poker : pokers) { System.out.print(poker + " "); i++; if (i % 13 == 0) { System.out.println(); } } } } } ~~~
                  <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>

                              哎呀哎呀视频在线观看