• <em id="6vhwh"><rt id="6vhwh"></rt></em>

    <style id="6vhwh"></style>

    <style id="6vhwh"></style>
    1. <style id="6vhwh"></style>
        <sub id="6vhwh"><p id="6vhwh"></p></sub>
        <p id="6vhwh"></p>
          1. 国产亚洲欧洲av综合一区二区三区 ,色爱综合另类图片av,亚洲av免费成人在线,久久热在线视频精品视频,成在人线av无码免费,国产精品一区二区久久毛片,亚洲精品成人片在线观看精品字幕 ,久久亚洲精品成人av秋霞

            arraylist排序(arraylist排序問題)

            更新時(shí)間:2023-02-28 21:31:09 閱讀: 評論:0

            arraylist數(shù)組排序

            用Collections工具方法

            importjava.util.Collections;
            importjava.util.Comparator;
            //按薪水由低到高排序
            publicvoidsort(){
            Collections.sort(arr,newComparator(){

            publicintcompare(Objecto1,Objecto2){
            Empe1=(Emp)o1;
            Empe2=(Emp)o2;

            returnFloat.compare(e1.sla,e2.sla);
            }
            });
            }

            如何實(shí)現(xiàn)對ArrayList排序 sort

            使用Collections.sort()傳入ArrayList,會采用默認(rèn)的方式進(jìn)行排序(字典序)
            使用Collections.sort()傳入ArrayList和自己實(shí)現(xiàn)Commparator接口的類的對象,實(shí)現(xiàn)自定義排序
            使用List.sort()傳入自己實(shí)現(xiàn)Commparator接口的類的對象,實(shí)現(xiàn)自定義排序
            Comparator返回值在jdk1.7、jdk1.8里必須是一對相反數(shù),如1和-1,不能是1和0.因?yàn)?.7的排序算法采用timsort,對返回值有嚴(yán)格要求

            如何實(shí)現(xiàn)對ArrayList排序 sort

            package com.collection;

            import java.util.ArrayList;
            import java.util.Collections;
            import java.util.Comparator;
            import java.util.List;

            public class Test {
            public static void main(String[] args) {

            Student zlj = new Student("丁曉宇", 21);
            Student dxy = new Student("趙四", 22);
            Student cjc = new Student("張三", 11);
            Student lgc = new Student("劉武", 19);

            List<Student> studentList = new ArrayList<Student>();
            studentList.add(zlj);
            studentList.add(dxy);
            studentList.add(cjc);
            studentList.add(lgc);

            System.out.println("按年齡升序:");
            Collections.sort(studentList, new SortByAge());
            for (Student student : studentList) {
            System.out.println(student.getName() + " / " + student.getAge());
            }
            System.out.println();
            System.out.println("按姓名排序:");
            Collections.sort(studentList, new SortByName());
            for (Student student : studentList) {
            System.out.println(student.getName() + " / " + student.getAge());
            }
            }
            }

            class SortByAge implements Comparator {
            public int compare(Object o1, Object o2) {
            Student s1 = (Student) o1;
            Student s2 = (Student) o2;
            return s1.getAge().compareTo(s2.getAge());
            // if (s1.getAge() > s2.getAge())
            // return 1;
            // return -1;
            }
            }

            class SortByName implements Comparator {
            public int compare(Object o1, Object o2) {
            Student s1 = (Student) o1;
            Student s2 = (Student) o2;
            return s1.getName().compareTo(s2.getName());
            }
            }

            輸出結(jié)果:
            按年齡升序:
            張三 / 11
            劉武 / 19
            丁曉宇 / 21
            趙四 / 22

            按姓名排序:
            丁曉宇 / 21
            劉武 / 19
            張三 / 11
            趙四 / 22

            java如何對Arraylist數(shù)組進(jìn)行排序(用comparable)

            看代碼:

            importjava.util.ArrayList;
            importjava.util.Arrays;
            importjava.util.Collections;

            publicclassDemo{

            publicstaticvoidmain(String[]args)throwsException{
            Pair[]pairs={
            newPair(0,1),
            newPair(2,9),
            newPair(7,0),
            newPair(8,8),
            newPair(8,6),
            newPair(9,2),
            newPair(1,5),
            newPair(8,2),
            newPair(9,15),
            newPair(9,5)
            };
            ArrayList<Pair>pairList=newArrayList<>(Arrays.asList(pairs));

            System.out.println("排序前:");
            System.out.println(Arrays.toString(pairs));
            Arrays.sort(pairs);//對數(shù)組排序
            System.out.println("排序后:");
            System.out.println(Arrays.toString(pairs));

            System.out.println("排序前:");
            System.out.println(pairList);
            Collections.sort(pairList);//對ArrayList排序
            System.out.println("排序后:");
            System.out.println(pairList);
            }
            }

            //繼承Comparable接口排序該類是“可排序的”
            //<>里面的是排序時(shí)與當(dāng)前實(shí)例進(jìn)行比較的實(shí)例的類型
            //一般都和當(dāng)前實(shí)例是同一個(gè)類型,比如這里就是Pair的實(shí)例和Pair的實(shí)例比較
            classPairimplementsComparable<Pair>{

            publicintleft;
            publicintright;

            publicPair(intleft,intright){
            this.left=left;
            this.right=right;
            }

            @Override
            publicStringtoString(){
            return"["+left+","+right+"]";
            }

            //排序規(guī)則,先按left排序,再按right排序
            @Override
            publicintcompareTo(Pairthat){
            if(this.left>that.left){
            return1;
            }elif(this.left<that.left){
            return-1;
            }elif(this.right>that.right){
            return1;
            }elif(this.right<that.right){
            return-1;
            }
            return0;
            }

            }

            可以發(fā)現(xiàn)先按 left 排序,如果 left 相等,則按 right 排序


            關(guān)于Java的ArrayList排序

            1、這段代碼是沒問題的,我試過了。
            2、“類型
            List
            中的方法
            add(int,
            Object)對于參數(shù)(int)不適用”,你是在什么地方看到的,java里好像沒有中文信息。這句話沒完全看懂,詳細(xì)說明一下add方法的用法吧。
            List的add(int
            index,
            Object
            obj)方法是在指定的索引index的位置插入一個(gè)對象obj,原index處和其后的對象依次后移一位。指定的index最大可以是List.size(),也就是最多可以把obj放到List的最后。index

            list.size()的時(shí)候會引發(fā)異常。
            方法的第一個(gè)參數(shù)類型是int,第二個(gè)是Object。jdk1.4和更早的版本里,第二個(gè)參數(shù)必須是Object。add(0,1)這種寫法是錯(cuò)誤的,必須用add(0,new
            Integer(1))。從jdk1.5開始,java加入一個(gè)重要的機(jī)制:自動拆裝箱,簡單的理解就是基本數(shù)據(jù)類型和其封裝類的自動轉(zhuǎn)化?,F(xiàn)在add(0,1)這種寫法是沒有錯(cuò)誤的。

            如何對ArrayList中的某個(gè)屬性進(jìn)行排序

            這個(gè)結(jié)果是降序的,升序的話條件換下
            Collections.sort(list, new Comparator<Map<String, Object>>() {

            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
            // TODO Auto-generated method stub
            int time1 = (Integer) o1.get("RemindTime");
            int time2 = (Integer) o2.get("RemindTime");
            if (time1 > time2) {
            return -1;
            } el if (time1 == time2) {
            return 0;
            } el {
            return 1;
            }
            }
            });

            本文發(fā)布于:2023-02-28 18:57:00,感謝您對本站的認(rèn)可!

            本文鏈接:http://www.newhan.cn/zhishi/a/167759106948117.html

            版權(quán)聲明:本站內(nèi)容均來自互聯(lián)網(wǎng),僅供演示用,請勿用于商業(yè)和其他非法用途。如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除。

            本文word下載地址:arraylist排序(arraylist排序問題).doc

            本文 PDF 下載地址:arraylist排序(arraylist排序問題).pdf

            下一篇:返回列表
            標(biāo)簽:arraylist
            相關(guān)文章
            留言與評論(共有 0 條評論)
               
            驗(yàn)證碼:
            Copyright ?2019-2022 Comsenz Inc.Powered by ? 實(shí)用文體寫作網(wǎng)旗下知識大全大全欄目是一個(gè)全百科類寶庫! 優(yōu)秀范文|法律文書|專利查詢|
            主站蜘蛛池模板: 国产精品成人高潮av| 欧美和黑人xxxx猛交视频| 国产十八禁在线观看免费| 精品无码国产一区二区三区AV| 日本免费最新高清不卡视频| 久久caoporn国产免费| 色九月亚洲综合网| 久久亚洲精品情侣| 国产一区二区不卡精品视频| www亚洲精品| 久9re热视频这里只有精品免费| 成人无码视频97免费| 97视频精品全国在线观看| 人妻精品中文字幕av| 五月av综合av国产av| 黄色国产精品一区二区三区| 精品少妇爆乳无码aⅴ区| 亚洲 中文 欧美 日韩 在线 | 国产在线精品国偷产拍| 亚洲中文在线观看午夜| 国产精品天天看天天狠| 琪琪777午夜理论片在线观看播放 国产成人亚洲精品日韩激情 | 日韩亚洲国产高清免费视频| 国产成人精品亚洲精品密奴| 亚洲人成亚洲人成在线观看| 波多野结衣av无码| 别揉我奶头~嗯~啊~的视频 | 粉嫩虎白女p虎白女在线| 99久久精品费精品国产一区二| 99精品国产在热久久| 99国产精品国产精品久久| 极品无码国模国产在线观看| 高清国产一区二区无遮挡| 亚洲三级香港三级久久| 亚洲性日韩精品一区二区三区| 亚洲精品乱码久久久久久中文字幕 | 成人国产一区二区三区精品| 白嫩少妇无套内谢视频| 无码熟妇人妻AV在线影片免费| 东方四虎在线观看av| 亚洲成人av在线高清|