Java common API java.util.Arrays (with analysis and examples) _06

java.util.Arrays directly inherit from the Object class.

The Arrays class contains various methods for manipulating arrays (such as sorting and searching), as well as a static factory that allows the array to be viewed as a list.

The documentation for the methods contained in this class all include a short description of the implementation. These descriptions should be considered implementation considerations, not part of the specification. Implementers should be free to substitute other algorithms as long as the specification itself is followed. (For example, the algorithm used by sort(Object[]) does not have to be a merge sort algorithm, but it must be stable.)

The Arrays class is a member of the Java Collections Framework.

-------------------------------------------------- ---Static method ----------------------------- ---

Static method (call directly with Arrays.xxx( ))

public static void sort(Object[ ] a)

Sorts the specified array of objects in ascending order according to the natural order of the elements (refer to the CompareTo method in the Comparable interface).

All elements in the array must implement the Comparable interface.

All elements in the array must be comparable to each other, such as comparing strings and numbers is not acceptable.

This ordering is guaranteed to be stable: equal elements are not reordered by calling the sort method.

The sorting algorithm is a modified merge sort algorithm (where the merge is ignored if the highest element in the low sublist is less than the lowest element in the high sublist).

This algorithm provides guaranteed n*log(n) performance.

public static void sort(Object[] a,  int fromIndex, int toIndex) Sorts the specified range of the specified array of objects in ascending order according to the natural ordering of the elements.
public static <T> void sort(T[ ] a, Comparator<? super T> c) Sorts the specified array of objects according to the order produced by the specified comparator.
public static <T> void sort(T[ ] a, int fromIndex, int toIndex, Comparator<? super T> c) Sorts the specified range of the specified array of objects according to the order produced by the specified comparator.

public static int binarySearch( Object[ ] a, Object key)

Use the binary search method to search the specified array to obtain the specified object.

Before making this call, the array must be sorted in ascending order according to the natural ordering of the elements. (via the sort(Object[], int, int) method). If the range is not sorted, the result is undefined.

If the array contains multiple elements equal to the specified object, there is no guarantee which one was found.

Returns the index of the search key if the element to be searched is contained in the array; otherwise returns (-(insertion point) - 1).

The insertion point is defined as the point at which the key is inserted into the array: the index of the first element greater than this key, or a.length if all elements in the array are less than the specified key.

public static int binarySearch(Object[ ] a, int fromIndex, int toIndex, Object key) Search the range of the specified array using the binary search method to obtain the specified object.
public static <T> int binarySearch(T[ ] a,  T key, Comparator<? super T> c)

Use the binary search method to search the specified array to obtain the specified object. Before making this call, the range must be sorted in ascending order according to the specified comparator (via the sort(T[ ], Comparator) method). If the range is not sorted, the result is undefined.

public static int binarySearch(Object[ ] a, int fromIndex, int toIndex, Comparator<? super T> c  ) Search the range of the specified array using the binary search method to obtain the specified object.
public static boolean equals(Object[ ] a, Object[ ] a2) 如果两个数组以相同顺序包含相同的元素,则两个数组是相等的。此外,如果两个数组引用都为 null,则认为它们是相等的。
public static void fill(Object[ ] a,Object val) 将指定的 Object 引用分配给指定 Object 数组的每个元素。
public static void fill(Object[ ] a, int fromIndex, int toIndex, Object val) 将指定的 Object 引用分配给指定 Object 数组指定范围中的每个元素。填充的范围从索引 
public static <T> T[ ] copyOf(T[ ] original, int newLength)

复制指定的数组,为使副本具有指定的长度,可能会用null填充。

所得数组与原数组属于相同的类

public static <T,U> T[ ] copyOf(U[] original, int newLength, Class<? extends T[]> newType)

复制指定的数组,为使副本具有指定的长度,可能会用null填充。

所得数组属于newType类。

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

将指定数组的指定范围复制到一个新数组。该范围的初始索引 (from) 必须位于 0 和 original.length(包括)之间。

所得数组与原数组属于相同的类。

public static <T,U> T[ ] copyOfRange(U[ ] original,int from, int to, Class<? extends T[ ]> newType)

将指定数组的指定范围复制到一个新数组。该范围的初始索引 (from) 必须位于 0 和 original.length(包括)之间。

所得数组属于newType类。

public static <T> List<T> asList(T... a) 返回一个受指定数组支持的固定大小的列表视图。(对返回列表的更改会“直接写”到数组。)此方法同 Collection.toArray() 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。
此方法还提供了一个创建固定长度的列表的便捷方法,该列表被初始化为包含多个元素:
例:List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
public static int hashCode(Object[ ] a)

基于指定数组的内容返回哈希码。如果数组包含其他数组,则哈希码将基于其标识,而不是基于其内容。

此方法返回的值等于 Arrays.asList(a).hashCode() 返回的值,除非 a 为 null,在这种情况下返回 0。

public static int deepHashCode(Object[ ] a) 基于指定数组的“深层内容”返回哈希码。如果数组包含作为元素的其他数组,则哈希码将基于其内容,并以此类推,直至无穷。这种调用的行为是不确定的。
public static boolean deepEquals(Object[] a1, Object[ ] a2) 如果两个指定数组彼此是深层相等 的,则返回 true。与 equals(Object[],Object[]) 方法不同,此方法适用于任意深度的嵌套数组。
public static String toString(Object[ ] a) 返回指定数组内容的字符串表示形式。如果数组包含作为元素的其他数组,则通过从 Object 中继承的 Object.toString() 方法将它们转换为字符串,这描述了它们的标识,而不是它们的内容。
public static String deepToString(Object[ ] a)

返回指定数组“深层内容”的字符串表示形式。如果数组包含作为元素的其他数组,则字符串表示形式包含其内容等。

此方法是为了将多维数组转换为字符串而设计的。

字符串表示形式由数组的元素列表组成,括在方括号("[ ]")中。相邻元素用字符 ", "(逗号加空格)分隔。这些元素通过 String.valueOf(Object) 转换为字符串,除非它们是自身的数组。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691925&siteId=291194637