數據算法--Arrays的使用


Java提供的Arrays類裏包含了一些static修飾的方法可以直接操作數組,這個Arrays類裏包含了如下幾個static修飾的方法:

1),static type binarySearch(type[] a, type key)

Searches the specified array of bytes for the specified value using the binary search algorithm.

使用二分法查詢key元素值在a數組中出現的索引,如果a數組不包含key元素值,則返回負數(-數組的長度-1)

調用該方法時要求數組中元素已經按升序排列,這樣才能得到正確結果

2),static type binarySearch(type[] a, int fromIndex, int toIndex, type key)

Searches a range of the specified array of bytes for the specified value using the binary search algorithm.

該方法與前一個方法類型,但它只搜索a數組中的fromIndex到toIndex索引的元素。

調用該方法時要求數組中元素已經按升序排列,這樣才能得到正確結果。

import java.util.Arrays;

/**
 * Arrays工具類的使用
 * 
 * @author LinkinPark
 */
public class ArraysTest
{

	public static void main(String[] args)
	{
		int[] arr = { 1, 2, 4, 3, 5 };
		// 使用binarySearch,要先對數組做升序排序
		Arrays.sort(arr);
		// 數組升序排序,下行代碼輸出[1, 2, 3, 4, 5]
		System.out.println(Arrays.toString(arr));
		// 4出現的索引爲3,下行代碼輸出3
		System.out.println(Arrays.binarySearch(arr, 4));
		// 數組不包含6,輸出-arr.length-1,下行代碼輸出-6
		System.out.println(Arrays.binarySearch(arr, 6));
		// 數組索引從0到2,查找1的索引,下面代碼輸出0
		System.out.println(Arrays.binarySearch(arr, 0, 2, 1));
		// 索引從0到2的數組不包含6,輸出-arr.length-1,下行代碼輸出-3
		System.out.println(Arrays.binarySearch(arr, 0, 2, 6));
	}

}

3),static <T> T[] copyOf(T[] original, int newLength)

Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.

這個方法將會把original數組複製成一個新數組,其中length是新數組的長度。

如果length小於original數組的長度,則新數組就是原來數組的前面length個元素

如果length大於original數組的長度,則新數組的前面元素就是原數組的所有元素,後面補充0,false,或者null

4),static <T> T[] copyOfRange(T[] original, int from, int to)

Copies the specified range of the specified array into a new array.

這個方法與前面方法相似,但這個方法值複製original數組的from索引到to索引的元素。

import java.util.Arrays;

/**
 * Arrays工具類的使用
 * 
 * @author LinkinPark
 */
public class ArraysTest
{

	public static void main(String[] args)
	{
		int[] arr = { 1, 2, 3, 4, 5 };
		// 新的數組長度大於arr數組,後面用0補齊。[1, 2, 3, 4, 5, 0]
		int[] copyArr1 = Arrays.copyOf(arr, 6);
		System.out.println(Arrays.toString(copyArr1));
		// 新的數組長度小於arr數組,截取新數組的長度。[1, 2, 3]
		int[] copyArr2 = Arrays.copyOf(arr, 3);
		System.out.println(Arrays.toString(copyArr2));
		// 截取arr數組下標從1到3形成一個新的數組。[2, 3]
		int[] copyArr3 = Arrays.copyOfRange(arr, 1, 3);
		System.out.println(Arrays.toString(copyArr3));
	}

}
5),static void fill(type[] a, type val)

Assigns the specified int value to each element of the specified array of ints.

該方法將會把a數組的所有元素都賦值爲val。

6),static void fill(type[] a, int fromIndex, int toIndex, type val)

Assigns the specified int value to each element of the specified range of the specified array of ints.

該方法與前一個方法的作用相同,區別只是該方法僅僅將a數組的fromIndex到toIndex索引的數組元素賦值爲val。

import java.util.Arrays;

/**
 * Arrays工具類的使用
 * 
 * @author LinkinPark
 */
public class ArraysTest
{

	public static void main(String[] args)
	{
		int[] arr = { 1, 2, 3, 4, 5 };
		// 將數組arr全部賦值爲6。[6, 6, 6, 6, 6]
		Arrays.fill(arr, 6);
		System.out.println(Arrays.toString(arr));
		int[] arr1 = { 1, 2, 3, 4, 5 };
		// 將數組arr1,索引從1到4,賦值爲6 [1, 6, 6, 6, 5]
		Arrays.fill(arr1, 1, 4, 6);
		System.out.println(Arrays.toString(arr1));
	}

}

7),static boolean equals(type[] a, type[] a2)

Returns true if the two specified arrays of ints are equal to one another.

如果a數組和a2數組的長度相等,而且a數組和a2數組的數組元素也一一相同,該方法將返回true。

8),static String toString(type[] a)

Returns a string representation of the contents of the specified array.

該方法將一個數組轉換成一個字符串,該方法按順序把多個數組元素連綴在一起,多個數組元素使用英文逗號和空格隔開

9),static void sort(type[] a)

Sorts the specified array into ascending numerical order.

該方法對a數組的數組元素進行排序。

10),static <T> List<T> asList(T... a)

Returns a fixed-size list backed by the specified array.

返回一個受指定數組支持的固定大小的列表,(對返回列表的更改會“直接寫”到數組。)此方法同 Collection.toArray() 一起,充當了基於數組的 API 與基於 collection 的 API 之

間的橋樑。返回的列表是可序列化的,並且實現了 RandomAccess。

此方法還提供了一個創建固定長度的列表的便捷方法,該列表被初始化爲包含多個元素:List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

import java.util.Arrays;
import java.util.List;

/**
 * Arrays工具類的使用
 * 
 * @author LinkinPark
 */
public class ArraysTest
{

	public static void main(String[] args)
	{
		// 判斷arr1和arr2是否相等,下面代碼輸出true
		int[] arr1 = { 1, 2, 3, 4, 5 };
		int[] arr2 = { 1, 2, 3, 4, 5 };
		System.out.println(Arrays.equals(arr1, arr2));
		// 將一個可變參數的數組轉換成一個數組,[1, 2, 3, 4, 5]
		List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
		System.out.println(list.toString());
	}

}

11),static XxxStream stream(xxx[] array)

Returns a sequential IntStream with the specified array as its source.

該方法將數組轉換爲Stream,Stream是Java8新增的流式編程的API。

12),static XxxStream stream(xxx[] array, int startInclusive, int endExclusive)

Returns a sequential IntStream with the specified range of the specified array as its source.

該方法與上一個方法相似,區別是該方法僅將startInclusive到endExclusive索引的元素轉換成Stream。

13),還有一些以parallel(並行)開頭的方法都表示該方法可利用CPU並行的能力來提高性能,比如parallelSort等等。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章