www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > 單片機 > 架構(gòu)師社區(qū)
[導(dǎo)讀]作者:藍筆頭鏈接:https://www.jianshu.com/p/f3e64e70eb1b1.排序1.1數(shù)組排序(`java.util.Arrays`)1.1.1基本數(shù)據(jù)類型排序?qū)φ麄€數(shù)組排序public?static?void?sort(int[]?a);對部分數(shù)組[fro...

作者:藍筆頭
鏈接:https://www.jianshu.com/p/f3e64e70eb1b

1. 排序

1.1 數(shù)組排序(`java.util.Arrays`)

1.1.1 基本數(shù)據(jù)類型排序
  • 對整個數(shù)組排序

public?static?void?sort(int[]?a);
  • 對部分數(shù)組 [fromIndex, toIndex) 排序

public?static?void?sort(int[]?a,?int?fromIndex,?int?toIndex);
七種基本類型 int、long、short、charbyte、float、double(除了 boolean),都支持上述格式的排序 API。

1.1.2 對象排序
  • 實現(xiàn)了 java.lang.Comparable 接口的對象。

//?對整個數(shù)組排序
public?static?void?sort(Object[]?a);
//?對部分數(shù)組?[fromIndex,?toIndex)?排序
public?static?void?sort(Object[]?a,?int?fromIndex,?int?toIndex);
  • 通過 java.util.Comparator 排序:

void sort(T[] a, Comparator c); // 對部分數(shù)組 [fromIndex, toIndex) 排序 public staticvoid sort(T[] a, int fromIndex, int toIndex, Comparator c); public interface Comparator{ // result < 0:o1 排在 o2 前面 // result == 0:o1 和 o2 的值一樣 // result > 0:o1 排在 o2 后面 int compare(T o1, T o2); } " data-snippet-id="ext.3b4fd31d611c03007a2fb4e82c943c31" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?對整個數(shù)組排序
public?static??void?sort(T[]?a,?Comparatorsuper?T>?c);
//?對部分數(shù)組?[fromIndex,?toIndex)?排序
public?static??void?sort(T[]?a,?int?fromIndex,?int?toIndex,
????????????????????????????????Comparatorsuper
?T>?c)
;

public?interface?Comparator<T>?{
????// result
????// result ==?0:o1 和 o2 的值一樣
????// result >?0:o1 排在 o2 后面
????int?compare(T?o1,?T?o2);
}
案例:

() { @Override public int compare(Person o1, Person o2) { return o1.id - o2.id; } }); // 輸出: // Solution.Person(id=1) // Solution.Person(id=2) Arrays.stream(persons).forEach(System.out::println); } @AllArgsConstructor @ToString public static class Person { private int id; } } " data-snippet-id="ext.9f53044395f3063f1ff57b840ee3a007" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public?class?Solution?{
????public?static?void?main(String[]?args)?{
????????Person[]?persons?=?new?Person[2];
????????persons[0]?=?new?Person(2);
????????persons[1]?=?new?Person(1);

????????Arrays.sort(persons,?new?Comparator()?{
????????????@Override
????????????public?int?compare(Person?o1,?Person?o2)?{
????????????????return?o1.id?-?o2.id;
????????????}
????????});

????????//?輸出:
????????//?Solution.Person(id=1)
????????//?Solution.Person(id=2)
????????Arrays.stream(persons).forEach(System.out::println);
????}

????@AllArgsConstructor
????@ToString
????public?static?class?Person?{
????????private?int?id;
????}
}
或者使用 lambda 表達式替換 Comparator 匿名類。

{ return o1.id - o2.id; }); " data-snippet-id="ext.4973bc6f1d0213eb013aab093375a4f4" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">????????Arrays.sort(persons,?(o1,?o2)?->?{
????????????return?o1.id?-?o2.id;
????????});

1.2 列表排序(`java.util.Collections`)

  • 排序

> void sort(Listlist); public staticvoid sort(Listlist, Comparator c); public interface Comparator{ // result < 0:o1 排在 o2 前面 // result == 0:o1 和 o2 的值一樣 // result > 0:o1 排在 o2 后面 int compare(T o1, T o2); } " data-snippet-id="ext.48d424bc07d1782333e834b6ae0da4ae" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public?static?super?T>>?void?sort(List?list);

public?static??void?sort(List?list,?Comparatorsuper?T>?c);

public?interface?Comparator<T>?{
????// result
????// result ==?0:o1 和 o2 的值一樣
????// result >?0:o1 排在 o2 后面
????int?compare(T?o1,?T?o2);
}
  • 反轉(zhuǎn)列表元素

public?static?void?reverse(List?list);

1.3 二維數(shù)組排序(`java.util.Arrays`)

提示:Java 數(shù)組也是一種對象

void sort(T[] a, Comparator c); // 案例 Arrays.sort(nums, (int[]a, int[]b) -> a[0] - b[0]); " data-snippet-id="ext.d85d29980807199ad798d55caa97768a" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?api
public?static??void?sort(T[]?a,?Comparatorsuper?T>?c);

//?案例
Arrays.sort(nums,?(int[]a,?int[]b)?->?a[0]?-?b[0]);

2. 二分查找

  • 數(shù)組(java.util.Arrays

int binarySearch(T[] a, T key, Comparator c); public staticint binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator c); " data-snippet-id="ext.f23c7e0970895182c737a49fa0ca175a" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public?static?int?binarySearch(int[]?a,?int?key);
public?static?int?binarySearch(int[]?a,?int?fromIndex,?int?toIndex,?int?key);

public?static?int?binarySearch(Object[]?a,?Object?key);
public?static?int?binarySearch(Object[]?a,?int?fromIndex,?int?toIndex,?Object?key);

public?static??int?binarySearch(T[]?a,?T?key,?Comparatorsuper?T>?c);
public?static??int?binarySearch(T[]?a,?int?fromIndex,?int?toIndex,?T?key,?Comparatorsuper?T>?c);
  • 列表(java.util.Collections

int binarySearch(List> list, T key); public staticint binarySearch(List list, T key, Comparator c); " data-snippet-id="ext.f8d1fe37f82f9bfa0e5240865d0bfa44" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public?static??int?binarySearch(List?extends?Comparable?super?T>>?list,?T?key);

public?static??int?binarySearch(List?extends?T>?list,?T?key,?Comparator?super?T>?c);

3. 棧(`java.util.Stack`)

  • 創(chuàng)建

Stack?stack?=?new?Stack<>();
  • 數(shù)據(jù)操作

//?往【?!坷锩嫣砑右粋€元素
public?E?push(E?item)

//?往【?!坷锩鎻棾鲆粋€元素
public?synchronized?E?pop()
;
  • 條件判斷

public?synchronized?boolean?isEmpty();

4. 隊列(`java.util.Queue`)

  • 創(chuàng)建(java.util.LinkedList

Queue?queue?=?new?LinkedList<>();
  • 數(shù)據(jù)操作

//?往【隊列】里面添加一個元素
boolean?add(E?e);

//?往【隊列】里面彈出一個元素
E?poll();
  • 條件判斷

boolean?isEmpty();

5. 堆(`java.util.PriorityQueue`)

提示:Java 里面的優(yōu)先隊列

  • 創(chuàng)建

minHeap = new PriorityQueue<>(); // 創(chuàng)建一個最大堆 PriorityQueuemaxHeap = new PriorityQueue<>(Comparator.reverseOrder()); " data-snippet-id="ext.b81abce1e0996d64116eee23b0b4a398" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?創(chuàng)建一個最小堆
PriorityQueue?minHeap?=?new?PriorityQueue<>();

//?創(chuàng)建一個最大堆
PriorityQueue?maxHeap?=?new?PriorityQueue<>(Comparator.reverseOrder());
  • 數(shù)據(jù)操作

//?往【堆】里面添加一個元素
public?boolean?add(E?e);

//?從【堆】里面彈出一個元素
public?E?poll();

其他工具

  • 降序排序(java.util.Comparator

reversed(); // 反轉(zhuǎn)一個 Comparable 的排序規(guī)則 // 比如從【升序】反轉(zhuǎn)為【降序】 public static> ComparatorreverseOrder(); " data-snippet-id="ext.5516f5b09794f57947cbef89383120cd" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?反轉(zhuǎn)一個?Comparator?的排序規(guī)則
//?比如從【升序】反轉(zhuǎn)為【降序】
default?Comparator?reversed();

//?反轉(zhuǎn)一個?Comparable?的排序規(guī)則
//?比如從【升序】反轉(zhuǎn)為【降序】
public?static?super?T>>?Comparator?reverseOrder();
  • 大數(shù)(java.math.BigInteger

//?創(chuàng)建一個大數(shù)
public?static?BigInteger?valueOf(long?val);

//?數(shù)據(jù)操作
public?BigInteger?add(BigInteger?val);
public?BigInteger?subtract(BigInteger?val);
public?BigInteger?multiply(BigInteger?val);
public?BigInteger?divide(BigInteger?val);
  • 集合(java.util.Collections

ListnCopies(int n, T o); // 反轉(zhuǎn)一個 list 的順序 public static void reverse(List list); " data-snippet-id="ext.f8838765f10b2f138e21a8d0a0215ea6" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?初始化一個具有?n?個相同元素?o?的?list
public?static??List?nCopies(int?n,?T?o);

//?反轉(zhuǎn)一個?list?的順序
public?static?void?reverse(List?list);

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動電源

在工業(yè)自動化蓬勃發(fā)展的當(dāng)下,工業(yè)電機作為核心動力設(shè)備,其驅(qū)動電源的性能直接關(guān)系到整個系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動勢抑制與過流保護是驅(qū)動電源設(shè)計中至關(guān)重要的兩個環(huán)節(jié),集成化方案的設(shè)計成為提升電機驅(qū)動性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機 驅(qū)動電源

LED 驅(qū)動電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個照明設(shè)備的使用壽命。然而,在實際應(yīng)用中,LED 驅(qū)動電源易損壞的問題卻十分常見,不僅增加了維護成本,還影響了用戶體驗。要解決這一問題,需從設(shè)計、生...

關(guān)鍵字: 驅(qū)動電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動電源的公式,電感內(nèi)電流波動大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計 驅(qū)動電源

電動汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動汽車的核心技術(shù)之一是電機驅(qū)動控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機驅(qū)動系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動汽車的動力性能和...

關(guān)鍵字: 電動汽車 新能源 驅(qū)動電源

在現(xiàn)代城市建設(shè)中,街道及停車場照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進步,高亮度白光發(fā)光二極管(LED)因其獨特的優(yōu)勢逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動電源 LED

LED通用照明設(shè)計工程師會遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動電源的電磁干擾(EMI)問題成為了一個不可忽視的挑戰(zhàn)。電磁干擾不僅會影響LED燈具的正常工作,還可能對周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機重量也有所下降,所以,現(xiàn)在的LED驅(qū)動電源

關(guān)鍵字: LED 驅(qū)動電源 開關(guān)電源

LED驅(qū)動電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動電源
關(guān)閉