定義 | |
type[] arrayName; | |
type arrayName[]; | |
type[][] arrayName2; | 定義二維數組 |
初始化 | |
arrayName = new type[]{v1, v2, ...} | 靜態初始化 |
arrayName2 = new type[][]{ar1, ...} | |
arrayName2 = {ar1, ar2, ...} | 簡化靜態初始化 |
arrayName = new type[length] | 動態初始化, 指定了數組的長度 |
整數類型: 初始值爲0 | |
浮點類型: 0.0 | |
字符類型: '\u0000' | |
布爾類型: false | |
引用類型: null | |
arrayName[0] | 取出數組第一個元素, 其它類推 |
ar.length | 獲取數組長度 |
Arrays類 | |||
屬性/方法 | 修飾 | 參數 | 說明 |
.binarySearch() | static int | type[] a, type key | 使用二分法查詢key元素值在a數組中出現的索引; 若是a數組不包含key元素, 則返回負數. (數組必須已經按升序排列) |
type[] a, int formIndex, int toIndex, type key | 同上, 不過只搜索a數組中fromIndex到toIndex索引的元素 | ||
.copyOf() | static type[] | type[] original, int length | 把original數組複製成一個新數組, 其中length是數組的長度. 若是length小於original數組的長度, 則新數組就是原數組前length個元素; 若大於, 則新數組是在原數組基礎上向後補充對應類型的默認值 |
.copyOfRange() | static type[] | type[] original, int from, int to | 複製original數組的from索引到to索引的元素到新數組 |
.equals() | static boolean | type[] a, type[] a2 | 若兩數組長度相同且元素也一一相同則返回true |
.fill() | static type[] | type[] a, type val | 把a數組的全部元素都賦值爲val |
type[] a, int fromIndex, int toIndex, type val | 只對範圍內的賦值 | ||
.sort() | static type[] | type[] a | 對a數組的元素進行排序 |
type[] a, int fromIndex, toIndex | 只對範圍內的進行排序 | ||
java 8 爲Arrays類增長的一些工具方法, 主要加強了併發能力 | |||
.parallelPrefix() | static void | xxx[] array, XxxBinaryOperator op | 使用op參數指定的計算公式計算獲得的結果做爲新的元素. op計算公式包括left、right兩個形參, 其中left表明數組中前一個索引處的元素, right表明數組中當前索引處的元素, 當計算第一個新組元素時, left的值默認爲1 |
xxx[] array, int fromIndex, int toIndex, XxxBinaryOperator op | 計算範圍內的 | ||
.setAll() | static void | xxx[] array, IntToXxxFunction generator | 使用指定的生成器(generator)爲全部數組元素設置值, 該生成器控制數組元素值得生成算法 |
.parallelSetAll() | static void | xxx[] array, IntToXxxFunction generator | 同上, 不過增長了並行能力 |
.parallelSort() | static void | 參考.sort() | 功能與.sort()相似, 不過增長了並行能力 |
.spliterator() | static Splitertor. OfXxx | xxx[] array | 將數組全部元素轉換成對應的Spliterator對象 |
xxx[] array, int startInclusive, int endExclusive | 只對範圍內的進行操做 | ||
.stream() | static XxxStream | 參考.spliterator() | 將數組轉換爲Stream, Stream是Java 8新增的流式編程的API |