<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 集合排序 ## 集合中基本數據類型排序 **使用 Collections.sort(list) 進行排序** 根據元素的*自然順序*對指定列表按升序進行排序。 ~~~ public class IntSort { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(3); list.add(5); list.add(8); list.add(1); list.add(4); System.out.print("排序前的數據: "); for(Integer it : list) { System.out.print(it + " "); } Collections.sort(list); System.out.print("排序后的數據: "); for(Integer it : list) { System.out.print(it + " "); } System.out.print("反轉后的數據: "); Collections.reverse(list); for(Integer it : list) { System.out.print(it + " "); } } } ~~~ ## 集合中字符串排序 > 字符串是按照其首字母對應的ASCII(Unicode)碼值進行排序的。 ~~~ import java.util.ArrayList; import java.util.Collections; import java.util.List; public class StringSort { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("orange"); list.add("red"); list.add("blue"); list.add("yellow"); System.out.print("排序前順序: "); for(String color : list) { System.out.print(color + " "); } Collections.sort(list); System.out.println(); System.out.print("排序后順序: "); for(String color : list) { System.out.print(color + " "); } } } ~~~ * * * **對于自定義的類我們將使用Comparator和Comparable接口進行排序** ## Comparator接口 * 是可以強行對某個對象進行整體排序的比較器 * 可以將Comparator接口作為參數傳遞給sort方法(如 Collections.sort 或 Arrays.sort) * Comparator接口存在int compare(T o1, T o2)方法, 用來比較排序的兩個參數: a. 如果 o1 < o2,返回負整數; b. 如果 o1 == o2,返回0; c. 如果 o1 > o2,返回正整數。 * boolean equals(Object obj) 指示某個其他對象是否“等于”此Comparator;此方法可以被Object類中的equals方法覆蓋,不必重寫。 **根據自定義的數據類型中的String類型,進行排序** ~~~ public class Student { private String name; private String code; private int age; 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; } public Student() { super(); } @Override public String toString() { return "Student [名字=" + name + ", 學號=" + code + ", 年齡=" + age + "]"; } } /** * 重寫compare方法,在此方法中描寫我們的排序依據 * @author LiXinRong * */ public class NameComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { String name1 = o1.getName(); String name2 = o2.getName(); //compareTo()方法 //如果參數字符串等于此字符串,則返回值 0; //如果此字符串按字典順序小于字符串參數,則返回一個小于 0 的值; //如果此字符串按字典順序大于字符串參數,則返回一個大于 0 的值。 //按名字升序排序 int reult = name1.compareTo(name2); //按名字降序排序 //int reult = name2.compareTo(name1); return reult; } } import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ClassSort { public static void main(String[] args) { //按學生的名字升序進行排序 Student qdd = new Student("仇冬冬","001",25); Student drm = new Student("丁潤萌","002",21); Student wc = new Student("王晨","003",25); Student lzj = new Student("劉子杰","004",22); Student qh = new Student("邱晗","005",22); List<Student> list = new ArrayList<Student>(); list.add(qdd); list.add(drm); list.add(wc); list.add(lzj); list.add(qh); System.out.println("排序前順序:"); for(Student stu : list) { System.out.println(stu); } //進行排序 Collections.sort(list, new NameComparator()); System.out.println("排序后順序:"); for(Student stu : list) { System.out.println(stu); } } } ~~~ **按年齡的降序排序** ~~~ import java.util.Comparator; public class AgeComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { //按年齡進行降序排序 int age1 = o1.getAge(); int age2 = o2.getAge(); int result = age2 - age1; return result; } } public class ClassSort { public static void main(String[] args) { Student qdd = new Student("仇冬冬","001",25); Student drm = new Student("丁潤萌","002",21); Student wc = new Student("王晨","003",25); Student lzj = new Student("劉子杰","004",22); Student qh = new Student("邱晗","005",22); List<Student> list = new ArrayList<Student>(); list.add(qdd); list.add(drm); list.add(wc); list.add(lzj); list.add(qh); System.out.println("排序前順序:"); for(Student stu : list) { System.out.println(stu); } ////按學生的年齡降序進行排序 Collections.sort(list, new AgeComparator()); System.out.println("排序后順序:"); for(Student stu : list) { System.out.println(stu); } } } ~~~ ## Comparable接口 * 此接口強行對實現它的每個類的對象進行整體排序; * 這種排序被稱為類的自然排序,類的compareTo方法被稱為它的自然比較方法; * 對于集合,通過調用Collections.sort方法進行排序; * 對于數組,通過調用Arrays.sort方法進行排序; * int compareTo(T o) 方法,該對象(實現Comparable接口的對象)小于、等于或大于指定對象(compareTo方法中的參數對象),則分別返回負整數、零或正整數。 ~~~ public class Student implements Comparable<Student> { private String name; private String code; private int age; 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; } public Student() { super(); } @Override public String toString() { return "Student [名字=" + name + ", 學號=" + code + ", 年齡=" + age + "]"; } @Override public int compareTo(Student stu) { int age = this.age; int stuAge = stu.age; int result = age - stuAge; return result; } } public class ClassSort { public static void main(String[] args) { Student qdd = new Student("仇冬冬","001",25); Student drm = new Student("丁潤萌","002",21); Student wc = new Student("王晨","003",25); Student lzj = new Student("劉子杰","004",22); Student qh = new Student("邱晗","005",22); List<Student> list = new ArrayList<Student>(); list.add(qdd); list.add(drm); list.add(wc); list.add(lzj); list.add(qh); System.out.println("排序前順序:"); for(Student stu : list) { System.out.println(stu); } ////按學生的年齡升序進行排序 Collections.sort(list); System.out.println("排序后順序:"); for(Student stu : list) { System.out.println(stu); } } } ~~~ ## 練習 1 問:在一個列表中存儲以下元素:red、blue、orange、yellow,要求打印集合中最大和最小的元素 ~~~ public class Demo { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("red"); list.add("blue"); list.add("orange"); list.add("yellow"); Collections.sort(list); /*for(String color : list) { System.out.println(color); }*/ System.out.println(list); System.out.println("==================="); System.out.println("最小的元素:"+list.get(0)); System.out.println("最大的元素:"+list.get(list.size() - 1)); } } ~~~ 2 問:如何去除list(存儲元素如下)集合中重復的元素并排序 ~~~ List<String> list = new ArrayList<String>(); list.add("1"); list.add("1"); list.add("22"); list.add("2"); list.add("3"); list.add("31"); list.add("3 1"); list.add(""); list.add("0"); list.add("\t"); ~~~ ~~~ public class Demo { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1"); list.add("1"); list.add("22"); list.add("2"); list.add("3"); list.add("31"); list.add("3 1"); list.add(""); list.add("0"); list.add("\t"); getSingleList(list); System.out.println(list); } /** * 對String集合進行去重并排序 * @param list */ public static void getSingleList(List<String> list) { //set集合:存儲的是不可重復元素 Set<String> set = new HashSet<String>(); //使用addAll方法,將list集合整體插入到set集合中,并利用set不可存儲相同元素的特性進行去重處理 set.addAll(list); //利用clear方法進行list中元素的清空 list.clear(); //將去重之后的元素插入到list集合中 list.addAll(set); //排序 Collections.sort(list); } } ~~~ 3 寫一個Student類, 包含屬性code\[1-30), grade\[1-6\], score\[0-100\], 所有屬性都是隨機生成(Math.random()方法,返回一個范圍\[0,1)的隨機數) 要求保存這二十位同學的信息,學號(code)不得相同, 并找出分數最高的同學和分數最低的同學, 最后打印輸出最高分和最低分同學信息. ~~~ public class Student implements Comparable<Student> { private int code; private int grade; private int score; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Student() { super(); } public Student(int code, int grade, int score) { super(); this.code = code; this.grade = grade; this.score = score; } @Override public String toString() { return "Student [code=" + code + ", grade=" + grade + ", score=" + score + "]"; } @Override public int compareTo(Student o) { return (this.score - o.getScore()); } } public class Demo { public static void main(String[] args) { List<Student> list = new ArrayList<Student>(); Set<Integer> set = new HashSet<Integer>(); while(set.size() <= 20) { int code = (int)(Math.random() * 29 + 1); set.add(code); } for(Integer code : set) { int grade = (int)(Math.random() * 6 + 1); int sroce = (int)(Math.random() * 100 + 1); list.add(new Student(code, grade, sroce)); } Collections.sort(list); System.out.println("=========最大最小值為========="); System.out.println(list.get(list.size() - 1)); System.out.println(list.get(0)); } } ~~~
                  <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>

                              哎呀哎呀视频在线观看