Arrays工具類 【vaynexiao】

String[] stringArray = { "a", "b", "c", "d", "e" };  
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));  
System.out.println(arrayList);// [a, b, c, d, e]
	
int[] array1 = new int[]{1, 2, 3, 4};
int[] array2 = new int[]{1, 2, 3, 4};
boolean b1 = Arrays.equals(array1, array2); //true 判斷兩個數組是否相等

int[] array1 = new int[]{1, 2, 3, 4};
System.out.println(Arrays.toString(array1));
// 輸出結果爲[1, 2, 3, 4]
// Arrays.deepToString()主要用於數組中還有數組的情況,
// 而Arrays.toString()則相反,對於Arrays.toString()而言,
// 當數組中有數組時,不會打印出數組中的內容,只會以地址的形式打印出來。

int[] array1 = new int[5]; 
Arrays.fill(array1, 1); //給指定數組的每個元素分配指定的值
System.out.println(Arrays.toString(array1));
// 輸出結果爲[1, 1, 1, 1, 1]
// Arrays.sort(int[] a, int fromIndex, int toIndex) 也可以增加參數指定要分配的值的範圍

int[] array = new int[]{99, 23, 33, 0, 65, 9, 16, 84};
Arrays.sort(array);//按默認升序對指定數組進行排序
System.out.println(Arrays.toString(array));
// 輸出結果爲[0, 9, 16, 23, 33, 65, 84, 99]

// binarySearch(int[] a, int value):使用二分搜索算法在指定的數組中搜索指定的值,
// 並返回該值所在索引位置;若查詢不到,則返回-1
int[] array = new int[]{1, 17, 20, 44, 45, 62, 79, 88, 93};
int i = Arrays.binarySearch(array, 44);
System.out.println(i);
// 輸出結果爲3

boolean b = Arrays.asList(stringArray).contains("a");

int[] intArray = { 1, 2, 3, 4, 5 };  
int[] intArray2 = { 6, 7, 8, 9, 10 };  
// Apache Commons Lang 庫  
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

Set<String> set = new HashSet<String>(Arrays.asList(stringArray));  

int[] intArray = { 1, 2, 3, 4, 5 };  
ArrayUtils.reverse(intArray); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章