比较器Compare升序降序
生活随笔
收集整理的這篇文章主要介紹了
比较器Compare升序降序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
package com.itheima.d1_set; /*判斷比較器compare升序降序 */import sun.plugin.javascript.navig.Array;import java.util.Arrays; import java.util.Comparator;public class SetDemo4 {public static void main(String[] args) {Integer array[] ={10,3,7,2,20,24};Arrays.sort(array,new Comparator<Integer>(){@Overridepublic int compare(Integer t1, Integer t2) {return t1-t2; //升序}});for (Integer arrays : array){System.out.print(arrays+" ");}} }結果:2 3 7 10 20 24 package com.itheima.d1_set;import sun.plugin.javascript.navig.Array;import java.util.Arrays; import java.util.Comparator;public class SetDemo4 {public static void main(String[] args) {Integer array[] ={10,3,7,2,20,24};Arrays.sort(array, new Comparator<Integer>() {@Overridepublic int compare(Integer t1, Integer t2) {return t2-t1; //降序}});for (Integer arrays : array){System.out.print(arrays+" ");}} }結果:24 20 10 7 3 2發現:public int compare(Integer t1, Integer t2) {
? ? ? ? ? ? ? ? return t1-t2; ? ?//升序
? ? ? ? ? ? }
????????????????public int compare(Integer t1, Integer t2) {
? ? ? ? ? ? ? ? return t2-t1; ? ?//降序
? ? ? ? ? ? }
即:當返回的參數名稱與形參名順序相同時,則為升序。反之為降序。
總結
以上是生活随笔為你收集整理的比较器Compare升序降序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信支付SDK接入APP
- 下一篇: oa提醒模块要素_用华天动力OA系统管理