日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

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

作者:藍(lán)筆頭
鏈接:https://www.jianshu.com/p/f3e64e70eb1b

1. 排序

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

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

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

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

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

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

void sort(T[] a, Comparator c); // 對(duì)部分?jǐn)?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;">//?對(duì)整個(gè)數(shù)組排序
public?static??void?sort(T[]?a,?Comparatorsuper?T>?c);
//?對(duì)部分?jǐn)?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 表達(dá)式替換 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ù)組也是一種對(duì)象

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ù)操作

//?往【棧】里面添加一個(gè)元素
public?E?push(E?item)

//?往【棧】里面彈出一個(gè)元素
public?synchronized?E?pop()
;
  • 條件判斷

public?synchronized?boolean?isEmpty();

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

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

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

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

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

boolean?isEmpty();

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

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

  • 創(chuàng)建

minHeap = new PriorityQueue<>(); // 創(chuàng)建一個(gè)最大堆 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)建一個(gè)最小堆
PriorityQueue?minHeap?=?new?PriorityQueue<>();

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

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

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

其他工具

  • 降序排序(java.util.Comparator

reversed(); // 反轉(zhuǎn)一個(gè) 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)一個(gè)?Comparator?的排序規(guī)則
//?比如從【升序】反轉(zhuǎn)為【降序】
default?Comparator?reversed();

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

//?創(chuàng)建一個(gè)大數(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)一個(gè) 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;">//?初始化一個(gè)具有?n?個(gè)相同元素?o?的?list
public?static??List?nCopies(int?n,?T?o);

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

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