深入了解Java核心類庫(kù)--Arrays類
Java常用類庫(kù)Arrays
類Arrays包含用于操作數(shù)組的各種方法(例如排序和搜索)
- 如果指定的數(shù)組引用為null,則此類中的方法都拋出NullPointerException ,除非另有說明
一、常用方法
1.1 toString
返回指定數(shù)組內(nèi)容的字符串形式
舉例
int[] a1 = {1,2,3,4,5}; System.out.println(Arrays.toString(a1));//[1, 2, 3, 4, 5]
源碼
public static String toString(int[] a) { if (a == null) return "null"; int iMax = a.length - 1; if (iMax == -1) return "[]"; StringBuilder b = new StringBuilder(); b.append('['); for (int i = 0; ; i++) { b.append(a[i]); if (i == iMax) return b.append(']').toString(); b.append(", "); } }
其它
Modifier and Type | Field | Description |
---|---|---|
static String | deepToString(Object[] a) | 返回指定數(shù)組的“深層內(nèi)容”的字符串表示形式 |
1.2 Sort
排序(默認(rèn)升序)
1.2.1 sort(T[] a, int fromIndex, int toIndex)
指定區(qū)間進(jìn)行排序
舉例
int[] a1 = {9,1,3,7,2,5}; System.out.println(Arrays.toString(a1));//[9, 1, 3, 7, 2, 5] Arrays.sort(a1,0,3);//[0,3),對(duì)9,1,3進(jìn)行排序 System.out.println(Arrays.toString(a1));//[0[1, 3, 9, 7, 2, 5]
源碼
public static void sort(int[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); }
1.2.2 Sort(T[] a)
對(duì)整個(gè)數(shù)組進(jìn)行排序
舉例
int[] a1 = {0,7,8,2,4,1}; System.out.println(Arrays.toString(a1));//[0, 7, 8, 2, 4, 1] Arrays.sort(a1); System.out.println(Arrays.toString(a1));//[0, 1, 2, 4, 7, 8]
源碼
public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); }
1.2.3 其它
Modifier and Type | Field | Description |
---|---|---|
static void | sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) | 根據(jù)指定比較器引發(fā)的順序?qū)χ付▽?duì)象數(shù)組的指定范圍進(jìn)行排序。 |
static void | sort(T[] a, Comparator<? super T> c) | 根據(jù)指定比較器引發(fā)的順序?qū)χ付ǖ膶?duì)象數(shù)組進(jìn)行排序。 |
static void | parallelSort(T[] a) | 將指定的數(shù)組按升序排序。 |
static void | parallelSort(T[] a, int fromIndex, int toIndex) | 將指定的數(shù)組范圍按數(shù)字升序排序。 |
static <T extends Comparable<? super T>>void | parallelSort(T[] a) | 根據(jù)元素的natural ordering對(duì)指定的對(duì)象數(shù)組按升序排序。 |
static <T extends Comparable<? super T>>void | parallelSort(T[] a, int fromIndex, int toIndex) | 根據(jù)元素的natural ordering ,將指定對(duì)象數(shù)組的指定范圍按升序排序。 |
static void | parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) | 根據(jù)指定比較器引發(fā)的順序?qū)χ付▽?duì)象數(shù)組的指定范圍進(jìn)行排序。 |
static void | parallelSort(T[] a, Comparator<? super T> cmp) | 根據(jù)指定比較器引發(fā)的順序?qū)χ付ǖ膶?duì)象數(shù)組進(jìn)行排序。 |
1.3 copyOf
復(fù)制(常用于數(shù)組擴(kuò)容)
舉例
int[] a = {1,2,3}; System.out.println(a.length);//output:3 a = Arrays.copyOf(a,15); System.out.println(a.length);//output:15
源碼
public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
其它
Modifier and Type | Field | Description |
---|---|---|
static T[] | copyOf(T[] original, int newLength) | 使用空值復(fù)制指定的數(shù)組,截?cái)嗷蛱畛洌ㄈ缬斜匾允垢北揪哂兄付ǖ拈L(zhǎng)度 |
static <T,U>T[] | copyOf(U[] original, int newLength, 類<? extends T[]> newType) | 使用空值復(fù)制指定的數(shù)組,截?cái)嗷蛱畛洌ㄈ缬斜匾?,以使副本具有指定的長(zhǎng)度 |
1.4 mismatch
舉例
int[] a1 = {0,1,2,3,4,5}; int[] a2 = {0,1,2,3,4,5};//與a1相同 int[] a3 = {0,1,2,3,0,5};//從索引4開始與a1不同 System.out.println(Arrays.mismatch(a1,a2));//output:-1 System.out.println(Arrays.mismatch(a1,a3));//output:4
源碼
public static int mismatch(int[] a, int[] b) { int length = Math.min(a.length, b.length); // Check null array refs if (a == b) return -1; int i = ArraysSupport.mismatch(a, b, length); return (i < 0 && a.length != b.length) ? length : i; }
其它
Modifier and Type | Field | Description |
---|---|---|
static int | mismatch(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) | 查找并返回指定范圍內(nèi)兩個(gè) Object數(shù)組之間第一個(gè)不匹配的相對(duì)索引,否則如果未找到不匹配則返回-1。 |
static int | mismatch(T[] a, T[] b, Comparator<? super T> cmp) | 查找并返回兩個(gè) Object數(shù)組之間第一個(gè)不匹配的索引,否則如果未找到不匹配則返回-1。 |
1.5 binarySearch
二分查找,搜索,返回下標(biāo)
1.5.1 binarySearch(T[] a, int fromIndex, int toIndex, T key)
限定了搜索的范圍[fromIndex, toIndex)
舉例
int[] a = {1,2,3,4,5}; int x1 = Arrays.binarySearch(a,2,3,4);//在a數(shù)組下標(biāo)[2,3)中查找值為4的下標(biāo) System.out.println(x1);//output:<0的隨機(jī)數(shù)
源碼
public static int binarySearch(int[] a, int fromIndex, int toIndex,int key) { rangeCheck(a.length, fromIndex, toIndex); return binarySearch0(a, fromIndex, toIndex, key); } private static int binarySearch0(int[] a, int fromIndex, int toIndex,int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. }
1.5.2 binarySearch(T[] a, T key)
與上述相同,只是沒有限定范圍,fromIndex=0, toIndex=length
舉例
int[] a = {1,2,3,4,5}; int x1 = Arrays.binarySearch(a,3);//在a數(shù)組中查找值為3的下標(biāo) int x2 = Arrays.binarySearch(a,-6);//在a數(shù)組中查找值為6的下標(biāo) System.out.println(x1);//output:2 System.out.println(x2);//output:<0的隨機(jī)數(shù)
源碼
public static int binarySearch(int[] a, int key) { return binarySearch0(a, 0, a.length, key); } private static int binarySearch0(int[] a, int fromIndex, int toIndex,int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. }
1.5.3 其它
Modifier and Type | Field | Description |
---|---|---|
static int | binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) | 使用二進(jìn)制搜索算法搜索指定對(duì)象的指定數(shù)組范圍 |
static int | binarySearch(T[] a, T key, Comparator<? super T> c) | 使用二進(jìn)制搜索算法在指定的數(shù)組中搜索指定的對(duì)象 |
1.6 equals
1.6.1 equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)
如果兩個(gè)指定數(shù)組在指定范圍內(nèi)相等 ,則返回true
舉例
int[] a1 = {1,2,3,4,5}; int[] a2 = {1,2,0,0,4,5}; System.out.println(Arrays.equals(a1,0,2,a2,0,2));//true System.out.println(Arrays.equals(a1,3,5,a2,4,6));//true
源碼
public static boolean equals(int[] a, int aFromIndex, int aToIndex,int[] b, int bFromIndex, int bToIndex) { rangeCheck(a.length, aFromIndex, aToIndex); rangeCheck(b.length, bFromIndex, bToIndex); int aLength = aToIndex - aFromIndex; int bLength = bToIndex - bFromIndex; if (aLength != bLength) return false; return ArraysSupport.mismatch(a, aFromIndex, b, bFromIndex,aLength) < 0; }
1.6.2 equals(T[] a, T[] a2)
如果兩個(gè)指定數(shù)組相等,則返回 true
舉例
int[] a1 = {1,2,3,4,5}; int[] a2 = {1,2,3,4,5}; int[] a3 = {1,2,0,4,5}; System.out.println(Arrays.equals(a1,a2));//true System.out.println(Arrays.equals(a1,a3));//false
源碼
public static boolean equals(int[] a, int[] a2) { if (a==a2) return true; if (a==null || a2==null) return false; int length = a.length; if (a2.length != length) return false; return ArraysSupport.mismatch(a, a2, length) < 0; }
1.6.3 其它
Modifier and Type | Field | Description |
---|---|---|
static boolean | deepEquals(Object[] a1, Object[] a2) | 如果兩個(gè)指定的數(shù)組彼此 深度相等 ,則返回 true |
static boolean | equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) | 如果在指定范圍內(nèi)指定的兩個(gè)Object數(shù)組彼此 相等 ,則返回true |
static boolean | equals(T[] a, T[] a2, Comparator<? super T> cmp) | 如果兩個(gè)指定的Objects數(shù)組彼此 相等 ,則返回 true |
1.7 fills
1.7.1 fill(T[] a, int fromIndex, int toIndex, T val)
將指定的T值分配給指定的T類型數(shù)組的指定范圍的每個(gè)元素
舉例
int[] a1 = new int[10]; Arrays.fill(a1,1,4,8); char[] a2 = new char[10]; Arrays.fill(a2,0,3,'s'); System.out.println(Arrays.toString(a1));//[0, 8, 8, 8, 0, 0, 0, 0, 0, 0] System.out.println(Arrays.toString(a2));//[s, s, s, , , , , , , ]
源碼
public static void fill(char[] a, int fromIndex, int toIndex, char val) { rangeCheck(a.length, fromIndex, toIndex); for (int i = fromIndex; i < toIndex; i++) a[i] = val; }
1.7.2 fill(T[] a, T val)
將指定的T值分配給指定的T類型數(shù)組的每個(gè)元素
舉例
int[] a1 = new int[10]; Arrays.fill(a1,8); char[] a2 = new char[10]; Arrays.fill(a2,'s'); System.out.println(Arrays.toString(a1));//[8, 8, 8, 8, 8, 8, 8, 8, 8, 8] System.out.println(Arrays.toString(a2));//[s, s, s, s, s, s, s, s, s, s]
源碼
public static void fill(int[] a, int val) { for (int i = 0, len = a.length; i < len; i++) a[i] = val; }
二、其他方法
Modifier and Type | Field | Description |
---|---|---|
static List | asList(T… a) | 返回由指定數(shù)組支持的固定大小的列表。 |
static int | compare(T[] a, T[] b) | 字典順序比較兩個(gè)T陣列 |
static int | compare(T[] a, int aFromIndex, int aToIndex,T[] b, int bFromIndex, int bToIndex) | 在指定范圍內(nèi)按字典順序比較兩個(gè)T陣列 |
static <T extends Comparable<? super T>>int | compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) | 在指定范圍內(nèi)按字典順序比較兩個(gè) Object陣列。 |
static int | compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) | 在指定范圍內(nèi)按字典順序比較兩個(gè) Object陣列。 |
static <T extends Comparable<? super T>>int | compare(T[] a, T[] b) | 按 Object順序比較兩個(gè) Object陣列,在可比元素中。 |
static int | compare(T[] a, T[] b, Comparator<? super T> cmp) | 使用指定的比較器按字典順序比較兩個(gè) Object陣列 |
static int | compareUnsigned(T[] a, T[] b) | byte字典順序比較兩個(gè)T陣列,數(shù)字處理元素為無符號(hào) |
static int | compareUnsigned(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) | 在指定范圍內(nèi)按字典順序比較兩個(gè) T陣列,將元素?cái)?shù)字處理為無符號(hào) |
static T[] | copyOfRange(T[] original, int from, int to) | 將指定數(shù)組的指定范圍復(fù)制到新數(shù)組中 |
static T[] | copyOfRange(T[] original, int from, int to) | 將指定數(shù)組的指定范圍復(fù)制到新數(shù)組中 |
static <T,U>T[] | copyOfRange(U[] original, int from, int to, 類<? extends T[]> newType) | 將指定數(shù)組的指定范圍復(fù)制到新數(shù)組中 |
static int | hashCode(T[] a) | 根據(jù)指定數(shù)組的內(nèi)容返回哈希碼 |
static int | deepHashCode(Object[] a) | 返回基于指定數(shù)組的“深層內(nèi)容”的哈希碼 |
static void | parallelPrefix(T[] array, int fromIndex, int toIndex,TBinaryOperator op) | 對(duì)于給定的數(shù)組子范圍執(zhí)行parallelPrefix(T[], TBinaryOperator) |
static void | parallelPrefix(dT[] array, TBinaryOperator op) | 使用提供的函數(shù)并行地累積給定數(shù)組的每個(gè)元素 |
static void | parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator op) | 對(duì)于給定的數(shù)組子范圍執(zhí)行 parallelPrefix(Object[], BinaryOperator) |
static void | parallelPrefix(T[] array, BinaryOperator op) | 使用提供的函數(shù)并行地累積給定數(shù)組的每個(gè)元素 |
static void | parallelSetAll(double[] array, IntToDoubleFunction generator) | 使用提供的生成器函數(shù)并行設(shè)置指定數(shù)組的所有元素以計(jì)算每個(gè)元素 |
static void | parallelSetAll(int[] array, IntUnaryOperator generator) | 使用提供的生成器函數(shù)并行設(shè)置指定數(shù)組的所有元素以計(jì)算每個(gè)元素 |
static void | parallelSetAll(long[] array, IntToLongFunction generator) | 使用提供的生成器函數(shù)并行設(shè)置指定數(shù)組的所有元素以計(jì)算每個(gè)元素 |
static void | parallelSetAll(T[] array, IntFunction<? extends T> generator) | 使用提供的生成器函數(shù)并行設(shè)置指定數(shù)組的所有元素以計(jì)算每個(gè)元素 |
static void | setAll(double[] array, IntToDoubleFunction generator) | 使用提供的生成器函數(shù)設(shè)置指定數(shù)組的所有元素以計(jì)算每個(gè)元素 |
static void | setAll(int[] array, IntUnaryOperator generator) | 使用提供的生成器函數(shù)設(shè)置指定數(shù)組的所有元素以計(jì)算每個(gè)元素 |
static void | setAll(long[] array, IntToLongFunction generator) | 使用提供的生成器函數(shù)設(shè)置指定數(shù)組的所有元素以計(jì)算每個(gè)元素 |
static void | setAll(T[] array, IntFunction<? extends T> generator) | 使用提供的生成器函數(shù)設(shè)置指定數(shù)組的所有元素以計(jì)算每個(gè)元素 |
static | Spliterator.OfDouble spliterator(double[] array) | 返回覆蓋所有指定數(shù)組的Spliterator.OfDouble |
static | Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) | 返回覆蓋指定數(shù)組的指定范圍的Spliterator.OfDouble |
static | Spliterator.OfInt spliterator(int[] array) | 返回覆蓋所有指定數(shù)組的Spliterator.OfInt |
static | Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) | 返回覆蓋指定數(shù)組的指定范圍的Spliterator.OfInt |
static | Spliterator.OfLong spliterator(long[] array) | 返回覆蓋所有指定數(shù)組的Spliterator.OfLong |
static | Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) | 返回覆蓋指定數(shù)組的指定范圍的Spliterator.OfLong |
static Spliterator | spliterator(T[] array) | 返回覆蓋所有指定數(shù)組的Spliterator |
static Spliterator | spliterator(T[] array, int startInclusive, int endExclusive) | 返回覆蓋指定數(shù)組的指定范圍的Spliterator |
static DoubleStream | stream(double[] array) | 返回以指定數(shù)組作為源的順序DoubleStream |
static DoubleStream | stream(double[] array, int startInclusive, int endExclusive) | 返回指定數(shù)組的指定范圍作為其源的順序DoubleStream |
static IntStream | stream(int[] array) | 返回以指定數(shù)組作為源的順序IntStream |
static IntStream | stream(int[] array, int startInclusive, int endExclusive) | 返回指定數(shù)組的指定范圍作為其源的順序IntStream |
static LongStream | stream(long[] array) | 返回以指定數(shù)組作為源的順序LongStream |
static LongStream | stream(long[] array, int startInclusive, int endExclusive) | 返回指定數(shù)組的指定范圍作為其源的順序LongStream |
static Stream | stream(T[] array) | 返回以指定數(shù)組作為源的順序Stream |
static Stream | stream(T[] array, int startInclusive, int endExclusive) | 返回指定數(shù)組的指定范圍作為其源的順序Stream |
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
以實(shí)例講解Objective-C中的KVO與KVC機(jī)制
這篇文章主要介紹了以實(shí)例講解Objective-C中的KVO與KVC機(jī)制,即Key-Value-Observing與Key-Value-Coding,需要的朋友可以參考下2015-09-09不到十行實(shí)現(xiàn)javaCV圖片OCR文字識(shí)別
識(shí)別圖片中的文字,會(huì)省很多時(shí)間,本文介紹了javaCV圖片OCR文字識(shí)別,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Spring?boot配置綁定和配置屬性校驗(yàn)的方式詳解
這篇文章主要介紹了Spring?boot配置綁定和配置屬性校驗(yàn),SpringBoot 提供了2 種方式進(jìn)行配置綁定,即使用 @ConfigurationProperties 注解和使用 @Value 注解,需要的朋友可以參考下2022-05-05Mybatis分頁查詢的實(shí)現(xiàn)(Rowbounds和PageHelper)
本文主要介紹了Mybatis分頁查詢的實(shí)現(xiàn)(Rowbounds和PageHelper),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01深入理解Java中的final關(guān)鍵字_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Java中的final關(guān)鍵字非常重要,它可以應(yīng)用于類、方法以及變量。這篇文章中我將帶你看看什么是final關(guān)鍵字以及使用final的好處,具體內(nèi)容詳情通過本文學(xué)習(xí)吧2017-04-04SpringBoot文件上傳大小設(shè)置方式(yml中配置)
這篇文章主要介紹了SpringBoot文件上傳大小設(shè)置方式(yml中配置),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容
這篇文章主要介紹了如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03快速解決List集合add元素,添加多個(gè)對(duì)象出現(xiàn)重復(fù)的問題
這篇文章主要介紹了快速解決List集合add元素,添加多個(gè)對(duì)象出現(xiàn)重復(fù)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08