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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## Collection -數組 存儲一個元素集合 Collection接口又有3種子類型,List、Set和Queue ### List-有序 [簡書參考網址](https://www.jianshu.com/p/25aa92f8d681) 對于需要快速插入,刪除元素,應該使用LinkedList。 對于需要快速隨機訪問元素,應該使用ArrayList。 對于“單線程環境” 或者 “多線程環境,但List僅僅只會被單個線程操作”,此時應該使用非同步的類(如ArrayList)。 ```java LinkedList linkedList = new LinkedList(); ArrayList arrayList = new ArrayList(); ``` ``` //設置Course 類 public class Course { public String id; public String name; public Course(String id,String name){ this.id = id; this.name = name; } } public class Listtest { //被選課程List public List coursesToSelect; public Listtest(){ this.coursesToSelect = new ArrayList(); } public void testAdd(){ //單個添加 Course cr1 = new Course("1","數據結構"); coursesToSelect.add(cr1); //調用List 的add方法把數據放入List中 Course cr2 = new Course("2","C語言"); coursesToSelect.add(0,cr2); //把cr2 放入最前面 //批量添加 Course[] course = {new Course("3","離散數學"),new Course("4","匯編語言")}; coursesToSelect.addAll(Arrays.asList(course)); //獲取coursesToSelect到某個值 Course temp = (Course) coursesToSelect.get(3); System.out.println("添加課程" + temp.id + ":" + temp.name); //通過迭代器來遍歷List Iterator it = coursesToSelect.iterator(); while(it.hasNext()){ Course temp = (Course) it.next(); System.out.println("迭代器輸出的添加課程" + temp.id + ":" + temp.name); } //2.用foreach 遍歷List for (Object obj :coursesToSelect){ Course cr = (Course) obj; System.out.println("foreach輸出的添加課程" + cr.id + ":" + cr.name); } //修改List的元素 coursesToSelect.set(3,new Course("7","Php")); //比較 //從Course 中取出的返回為true Course course = lists.get(0); System.out.println(lists.contains(course));//true //new 的值返回為false System.out.println(lists.contains(new Course("1","php")));//false , //如果類型為字符串,整數等類型,則可以lists.contains("hello") //刪除 coursesToSelect.remove(1); //排序 Collections.sort(coursesToSelect); //只能排字符串,整數范型 ,排列方式 0-9 -> A-Z -> a-z //批量刪除 Course[] courses = {(Course) coursesToSelect.get(0), (Course) coursesToSelect.get(3)}; coursesToSelect.removeAll(Arrays.asList(courses)); } } ``` #### 范型 即“參數化類型 1. 不使用范型 ``` public List coursesToSelect; public void testAdd(){ coursesToSelect.add("asdad");//在編輯器中不會報錯 } ``` 2. 使用范型 ``` public class Listtest{ public List<Course> coursesToSelect; //構造函數 public Listtest(){ //初始化coursesToSelect this.coursesToSelect = new ArrayList<String>(); } public void testAdd(){ coursesToSelect.add("asdad");//在編譯中報錯,方便查看錯誤 } public void testForEach(){ //由于規定了范型,所以直接轉為Course類型,并輸出,而不需要在轉換為Course類型 for (Course cr : coursesToSelect){ System.out.println(cr.id+":"+cr.name) } } } ``` 3. 如果需要創建字符串List ``` List<String> list = new ArrayList<String>(); list.add("qqyumidi"); ``` 4. 范型可以添加范型的子類型的對象實例 ``` public class CourseChild extends Course{ } ``` ``` CourseChild cr1 = new CourseChild(); cr1.id = "6"; cr1.name="C++"; coursesToSelect.add(cr1); //調用List 的add方法把數據放入List中 ``` 5. 范型集合中的限定類型不能使用基礎數據類型,但是可以通過使用包裝類限定允許存入的基本數據類型 ``` int -> Integer long -> Long boolean -> Boolean ``` demo ``` List<Integer> list= new ArrayList<Integer>(); list.add(12); System.out.println(list.get(0));//12 ``` ### set 無序 1. Set集合中沒有get(int index)方法,要獲取元素,使用Iterator迭代器或for循環。 2. 向Set集合中添加元素 可以使用add()方法,但是是無序且不可重復。 ``` Set<String> set = new HashSet<String>(); set.add("hello"); set.add("word"); set.add("!"); //Iterator 迭代器 Iterator it = set.iterator(); while(it.hasNext()){ System.out.println(it.next()); } //for 循環 for (String s: set) { System.out.println(s); } ``` ## Map - 鍵值對 [簡書參考網址](https://www.jianshu.com/p/5f9ba40fbc4d); ``` //賦值 HashMap<Integer,String> h = new HashMap<Integer,String>(); h.put(3, "heihei"); h.put(4, "haha"); h.put(8, "xyy"); //for循環 Set<Integer> s = h.keySet(); //獲取所有key for(Integer i:s){ System.out.println(i+","+h.get(i)); } //java8中的lamda表達式 map.forEach((k,v)->System.out.println( k + " : " + v)); //刪除 maps.remove(1); //修改 maps.put(3,"hello word"); //是否有某個key值 maps.containsKey(3); //是否包含某個value值 maps.containsValue("heihei"); ``` ## Comparable & Comparator 使某個類可比較,如把**人**這個類按身高比較 ### Comparable - 默認比較規則 1. 其實現類需實現compareTo()方法 2. compareTo() 方法返回正數表示大,負數表示小,0表示相等 ``` //繼承 Comparable接口,并實現compareTo()方法 public class Course implements Comparable<Course>{ public String id; public String name; public Course(String id,String name){ this.id = id; this.name = name; } @Override public int compareTo(Course course) { return this.id.compareTo(course.id); } } //調用 Course cr1 = new Course("4", "數據結構"); coursesToSelect.add(cr1); //調用List 的add方法把數據放入List中 Course cr2 = new Course("2", "C語言"); coursesToSelect.add(cr2); Collections.sort(coursesToSelect); //注意字符串類型比較 1000 回在200 簽名,是從第一個字符開始比較。 ``` ### Comparator - 臨時比較規則 1. 用于定義臨時比較規則,而不是默認比較規則 2. 其實現類需要實現compare()方法 ``` //定義一個Comparator 類 public class CouserComparator implements Comparator<Course> { @Override public int compare(Course course, Course t1) { return course.id.compareTo(t1.id); } } //調用 Collections.sort(coursesToSelect,new CouserComparator()); ```
                  <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>

                              哎呀哎呀视频在线观看