Arrays用法整理

2017-01-10 10:02:13来源:oschina作者:王孟君人点击


java.util.Arrays工具类包含了数组比较常用的方法,本文对该类进行整理并给出简单的示例,本文使用的JDK版本为1.7。



先来看一下,Arrays有哪些方法,然后,我们一个个地来玩一下。



获取固定大小的List

使用该方法可以返回一个固定大小的List。


源代码
/**
* Returns a fixed-size list backed by the specified array.(Changes to
* the returned list "write through" to the array.)This method acts
* as bridge between array-based and collection-based APIs, in
* combination with {@link Collection#toArray}.The returned list is
* serializable and implements {@link RandomAccess}.
*
*

This method also provides a convenient way to create a fixed-size
* list initialized to contain several elements:
*


* List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
*

*
* @param a the array by which the list will be backed
* @return a list view of the specified array
*/
@SafeVarargs
public static List asList(T... a) {
return new ArrayList<>(a);
}
示例
List greetings = Arrays.asList("Hello", "Hi", "Good morning");
//[Hello, Hi, Good morning]
System.out.println(greetings);

注意:


该方法返回的List是固定大小的,即不能进行添加、删除等操作。


更多信息请访问链接正确认识Arrays.asList方法。



二分查找

使用该方法可以对一个给定的数组进行二分查找,数组的元素可以是int、byte、char等,如下来看一下int的二分查找方法和示例。


源代码
/**
* Searches a range of
* the specified array of ints for the specified value using the
* binary search algorithm.
* The range must be sorted (as
* by the {@link #sort(int[], int, int)} method)
* prior to making this call.If it
* is not sorted, the results are undefined.If the range contains
* multiple elements with the specified value, there is no guarantee which
* one will be found.
*
* @param a the array to be searched
* @param fromIndex the index of the first element (inclusive) to be
* searched
* @param toIndex the index of the last element (exclusive) to be searched
* @param key the value to be searched for
* @return index of the search key, if it is contained in the array
*within the specified range;
*otherwise, (-(insertion point) - 1).The
*insertion point is defined as the point at which the
*key would be inserted into the array: the index of the first
*element in the range greater than the key,
*or toIndex if all
*elements in the range are less than the specified key.Note
*that this guarantees that the return value will be >= 0 if
*and only if the key is found.
* @throws IllegalArgumentException
*if {@code fromIndex > toIndex}
* @throws ArrayIndexOutOfBoundsException
*if {@code fromIndex < 0 or toIndex > a.length}
* @since 1.6
*/
public static int binarySearch(int[] a, int fromIndex, int toIndex,
int key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}
// Like public version, but without range checks.
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.
}

也可以在指定的某个区间内查找:


/**
* Searches a range of
* the specified array of ints for the specified value using the
* binary search algorithm.
* The range must be sorted (as
* by the {@link #sort(int[], int, int)} method)
* prior to making this call.If it
* is not sorted, the results are undefined.If the range contains
* multiple elements with the specified value, there is no guarantee which
* one will be found.
*
* @param a the array to be searched
* @param fromIndex the index of the first element (inclusive) to be
* searched
* @param toIndex the index of the last element (exclusive) to be searched
* @param key the value to be searched for
* @return index of the search key, if it is contained in the array
*within the specified range;
*otherwise, (-(insertion point) - 1).The
*insertion point is defined as the point at which the
*key would be inserted into the array: the index of the first
*element in the range greater than the key,
*or toIndex if all
*elements in the range are less than the specified key.Note
*that this guarantees that the return value will be >= 0 if
*and only if the key is found.
* @throws IllegalArgumentException
*if {@code fromIndex > toIndex}
* @throws ArrayIndexOutOfBoundsException
*if {@code fromIndex < 0 or toIndex > a.length}
* @since 1.6
*/
public static int binarySearch(int[] a, int fromIndex, int toIndex,
int key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}
// Like public version, but without range checks.
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.
}
示例
int[] intValues = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int index = Arrays.binarySearch(intValues, 8);
// 7
System.out.println(index);

在指定区间内查找。


int[] intValues = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int index = Arrays.binarySearch(intValues, 2, intValues.length, 5);
// 4
System.out.println(index);
拷贝元素

使用copyOf方法可以完成源数组的元素拷贝到新数组中去。



源代码
/**
* Copies the specified array, truncating or padding with nulls (if necessary)
* so the copy has the specified length.For all indices that are
* valid in both the original array and the copy, the two arrays will
* contain identical values.For any indices that are valid in the
* copy but not the original, the copy will contain null.
* Such indices will exist if and only if the specified length
* is greater than that of the original array.
* The resulting array is of exactly the same class as the original array.
*
* @param original the array to be copied
* @param newLength the length of the copy to be returned
* @return a copy of the original array, truncated or padded with nulls
* to obtain the specified length
* @throws NegativeArraySizeException if newLength is negative
* @throws NullPointerException if original is null
* @since 1.6
*/
public static T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}/**
* Copies the specified array, truncating or padding with nulls (if necessary)
* so the copy has the specified length.For all indices that are
* valid in both the original array and the copy, the two arrays will
* contain identical values.For any indices that are valid in the
* copy but not the original, the copy will contain null.
* Such indices will exist if and only if the specified length
* is greater than that of the original array.
* The resulting array is of the class newType.
*
* @param original the array to be copied
* @param newLength the length of the copy to be returned
* @param newType the class of the copy to be returned
* @return a copy of the original array, truncated or padded with nulls
* to obtain the specified length
* @throws NegativeArraySizeException if newLength is negative
* @throws NullPointerException if original is null
* @throws ArrayStoreException if an element copied from
* original is not of a runtime type that can be stored in
* an array of class newType
* @since 1.6
*/
public static T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

CopyOfRange拷贝指定范围内的元素。


/**
* Copies the specified range of the specified array into a new array.
* The initial index of the range (from) must lie between zero
* and original.length, inclusive.The value at
* original[from] is placed into the initial element of the copy
* (unless from == original.length or from == to).
* Values from subsequent elements in the original array are placed into
* subsequent elements in the copy.The final index of the range
* (to), which must be greater than or equal to from,
* may be greater than original.length, in which case
* null is placed in all elements of the copy whose index is
* greater than or equal to original.length - from.The length
* of the returned array will be to - from.
*


* The resulting array is of exactly the same class as the original array.
*
* @param original the array from which a range is to be copied
* @param from the initial index of the range to be copied, inclusive
* @param to the final index of the range to be copied, exclusive.
* (This index may lie outside the array.)
* @return a new array containing the specified range from the original array,
* truncated or padded with nulls to obtain the required length
* @throws ArrayIndexOutOfBoundsException if {@code from < 0}
* or {@code from > original.length}
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original is null
* @since 1.6
*/
public static T[] copyOfRange(T[] original, int from, int to) {
return copyOfRange(original, from, to, (Class) original.getClass());
}
/**
* Copies the specified range of the specified array into a new array.
* The initial index of the range (from) must lie between zero
* and original.length, inclusive.The value at
* original[from] is placed into the initial element of the copy
* (unless from == original.length or from == to).
* Values from subsequent elements in the original array are placed into
* subsequent elements in the copy.The final index of the range
* (to), which must be greater than or equal to from,
* may be greater than original.length, in which case
* null is placed in all elements of the copy whose index is
* greater than or equal to original.length - from.The length
* of the returned array will be to - from.
* The resulting array is of the class newType.
*
* @param original the array from which a range is to be copied
* @param from the initial index of the range to be copied, inclusive
* @param to the final index of the range to be copied, exclusive.
* (This index may lie outside the array.)
* @param newType the class of the copy to be returned
* @return a new array containing the specified range from the original array,
* truncated or padded with nulls to obtain the required length
* @throws ArrayIndexOutOfBoundsException if {@code from < 0}
* or {@code from > original.length}
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original is null
* @throws ArrayStoreException if an element copied from
* original is not of a runtime type that can be stored in
* an array of class newType.
* @since 1.6
*/
public static T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}


从上述源代码可以看出,Arrays的copyOf方法最终还是调用了System.arraycopy的方法。



示例
String[] greetings = { "Hello", "Hi", "Good morning", "Good afternoon" };
String[] copyGreetings = Arrays.copyOf(greetings, greetings.length);
//[Hello, Hi, Good morning, Good afternoon]
System.out.println(Arrays.toString(copyGreetings));
String[] copyofRangeGreetings = Arrays.copyOfRange(greetings, 0, greetings.length - 2);
//[Hello, Hi]
System.out.println(Arrays.toString(copyofRangeGreetings));
数组元素统一赋值

如果想给一个数组统一赋值,那么Arrays.fill将是一个不错的选择。



源代码
/**
* Assigns the specified int value to each element of the specified array
* of ints.
*
* @param a the array to be filled
* @param val the value to be stored in all elements of the array
*/
public static void fill(int[] a, int val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}

fill方法其实就是循环了一下数组,然后给每个位置上的元素赋予一个指定的值。



示例
int[] intValues = new int[10];
//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(intValues));
Arrays.fill(intValues, 100);
//[100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
System.out.println(Arrays.toString(intValues));
数组元素排序
源代码

如果想对一个数组的元素进行排序,那么使用Arrays.sort方法会很容易。



如下int数组的排序源代码:


/**
* Sorts the specified array into ascending numerical order.
*
*

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
* offers O(n log(n)) performance on many data sets that cause other
* quicksorts to degrade to quadratic performance, and is typically
* faster than traditional (one-pivot) Quicksort implementations.
*
* @param a the array to be sorted
*/
public static void sort(int[] a) {
DualPivotQuicksort.sort(a);
}
/**
* Sorts the specified array.
*
* @param a the array to be sorted
*/
public static void sort(int[] a) {
sort(a, 0, a.length - 1);
}
/**
* Sorts the specified range of the array.
*
* @param a the array to be sorted
* @param left the index of the first element, inclusive, to be sorted
* @param right the index of the last element, inclusive, to be sorted
*/
public static void sort(int[] a, int left, int right) {
// Use Quicksort on small arrays
if (right - left < QUICKSORT_THRESHOLD) {
sort(a, left, right, true);
return;
}
/*
* Index run[i] is the start of i-th run
* (ascending or descending sequence).
*/
int[] run = new int[MAX_RUN_COUNT + 1];
int count = 0; run[0] = left;
// Check if the array is nearly sorted
for (int k = left; k < right; run[count] = k) {
if (a[k] < a[k + 1]) { // ascending
while (++k <= right && a[k - 1] <= a[k]);
} else if (a[k] > a[k + 1]) { // descending
while (++k <= right && a[k - 1] >= a[k]);
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
}
} else { // equal
for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
if (--m == 0) {
sort(a, left, right, true);
return;
}
}
}
/*
* The array is not highly structured,
* use Quicksort instead of merge sort.
*/
if (++count == MAX_RUN_COUNT) {
sort(a, left, right, true);
return;
}
}
// Check special cases
if (run[count] == right++) { // The last run contains one element
run[++count] = right;
} else if (count == 1) { // The array is already sorted
return;
}
/*
* Create temporary array, which is used for merging.
* Implementation note: variable "right" is increased by 1.
*/
int[] b; byte odd = 0;
for (int n = 1; (n <<= 1) < count; odd ^= 1);
if (odd == 0) {
b = a; a = new int[b.length];
for (int i = left - 1; ++i < right; a[i] = b[i]);
} else {
b = new int[a.length];
}
// Merging
for (int last; count > 1; count = last) {
for (int k = (last = 0) + 2; k <= count; k += 2) {
int hi = run[k], mi = run[k - 1];
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
if (q >= hi || p < mi && a[p] <= a[q]) {
b[i] = a[p++];
} else {
b[i] = a[q++];
}
}
run[++last] = hi;
}
if ((count & 1) != 0) {
for (int i = right, lo = run[count - 1]; --i >= lo;
b[i] = a[i]
);
run[++last] = right;
}
int[] t = a; a = b; b = t;
}
}
示例
int[] intValues = { 1, 6, 5, 9, 3, 4, 2, 7, 8 };
//[1, 6, 5, 9, 3, 4, 2, 7, 8]
System.out.println(Arrays.toString(intValues));
Arrays.sort(intValues);
//[1, 2, 3, 4, 5, 6, 7, 8, 9]
System.out.println(Arrays.toString(intValues));


如果数组中的元素是对象,可以自己编写一个比较器(Comparator)来完成,Arrays.sort也提供了相关的方法可以调用,如:



/**
* Sorts the specified array of objects according to the order induced by
* the specified comparator.All elements in the array must be
* mutually comparable by the specified comparator (that is,
* {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
* for any elements {@code e1} and {@code e2} in the array).
*
*

This sort is guaranteed to be stable:equal elements will
* not be reordered as a result of the sort.
*
*

Implementation note: This implementation is a stable, adaptive,
* iterative mergesort that requires far fewer than n lg(n) comparisons
* when the input array is partially sorted, while offering the
* performance of a traditional mergesort when the input array is
* randomly ordered.If the input array is nearly sorted, the
* implementation requires approximately n comparisons.Temporary
* storage requirements vary from a small constant for nearly sorted
* input arrays to n/2 object references for randomly ordered input
* arrays.
*
*

The implementation takes equal advantage of ascending and
* descending order in its input array, and can take advantage of
* ascending and descending order in different parts of the the same
* input array.It is well-suited to merging two or more sorted arrays:
* simply concatenate the arrays and sort the resulting array.
*
*

The implementation was adapted from Tim Peters's list sort for Python
* (
* TimSort
).It uses techiques from Peter McIlroy's "Optimistic
* Sorting and Information Theoretic Complexity", in Proceedings of the
* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
* January 1993.
*
* @param a the array to be sorted
* @param c the comparator to determine the order of the array.A
* {@code null} value indicates that the elements'
* {@linkplain Comparable natural ordering} should be used.
* @throws ClassCastException if the array contains elements that are
*not mutually comparable using the specified comparator
* @throws IllegalArgumentException (optional) if the comparator is
*found to violate the {@link Comparator} contract
*/
public static void sort(T[] a, Comparator<? super T> c) {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a, c);
else
TimSort.sort(a, c);
}
/** To be removed in a future release. */
private static void legacyMergeSort(T[] a, Comparator<? super T> c) {
T[] aux = a.clone();
if (c==null)
mergeSort(aux, a, 0, a.length, 0);
else
mergeSort(aux, a, 0, a.length, 0, c);
}
输出内容


Arrays工具类提供了toString和deepToString方法,用于更好地输出数组中的元素。



如果一个int数组,int[] intValues = { 1, 6, 5, 9, 3, 4, 2, 7, 8 };


直接使用System.out.println(intValues);去尝试输出内容,将得到一个类似[I@1453ecec的结果。这个时候,使用Arrays.toString可以帮助我们输出数组内容,如果是多维数组,则需要使用deepToString来输出。


源代码
/**
* Returns a string representation of the contents of the specified array.
* If the array contains other arrays as elements, they are converted to
* strings by the {@link Object#toString} method inherited from
* Object, which describes their identities rather than
* their contents.
*
*

The value returned by this method is equal to the value that would
* be returned by Arrays.asList(a).toString(), unless a
* is null, in which case "null" is returned.
*
* @param a the array whose string representation to return
* @return a string representation of a
* @see #deepToString(Object[])
* @since 1.5
*/
public static String toString(Object[] 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(String.valueOf(a[i]));
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}

deepToString的源码如下:


/**
* Returns a string representation of the "deep contents" of the specified
* array.If the array contains other arrays as elements, the string
* representation contains their contents and so on.This method is
* designed for converting multidimensional arrays to strings.
*
*

The string representation consists of a list of the array's
* elements, enclosed in square brackets ("[]").Adjacent
* elements are separated by the characters ", " (a comma
* followed by a space).Elements are converted to strings as by
* String.valueOf(Object), unless they are themselves
* arrays.
*
*

If an element e is an array of a primitive type, it is
* converted to a string as by invoking the appropriate overloading of
* Arrays.toString(e).If an element e is an array of a
* reference type, it is converted to a string as by invoking
* this method recursively.
*
*

To avoid infinite recursion, if the specified array contains itself
* as an element, or contains an indirect reference to itself through one
* or more levels of arrays, the self-reference is converted to the string
* "[...]".For example, an array containing only a reference
* to itself would be rendered as "[[...]]".
*
*

This method returns "null" if the specified array
* is null.
*
* @param a the array whose string representation to return
* @return a string representation of a
* @see #toString(Object[])
* @since 1.5
*/
public static String deepToString(Object[] a) {
if (a == null)
return "null";
int bufLen = 20 * a.length;
if (a.length != 0 && bufLen <= 0)
bufLen = Integer.MAX_VALUE;
StringBuilder buf = new StringBuilder(bufLen);
deepToString(a, buf, new HashSet());
return buf.toString();
}
private static void deepToString(Object[] a, StringBuilder buf,
Set dejaVu) {
if (a == null) {
buf.append("null");
return;
}
int iMax = a.length - 1;
if (iMax == -1) {
buf.append("[]");
return;
}
dejaVu.add(a);
buf.append('[');
for (int i = 0; ; i++) {
Object element = a[i];
if (element == null) {
buf.append("null");
} else {
Class eClass = element.getClass();
if (eClass.isArray()) {
if (eClass == byte[].class)
buf.append(toString((byte[]) element));
else if (eClass == short[].class)
buf.append(toString((short[]) element));
else if (eClass == int[].class)
buf.append(toString((int[]) element));
else if (eClass == long[].class)
buf.append(toString((long[]) element));
else if (eClass == char[].class)
buf.append(toString((char[]) element));
else if (eClass == float[].class)
buf.append(toString((float[]) element));
else if (eClass == double[].class)
buf.append(toString((double[]) element));
else if (eClass == boolean[].class)
buf.append(toString((boolean[]) element));
else { // element is an array of object references
if (dejaVu.contains(element))
buf.append("[...]");
else
deepToString((Object[])element, buf, dejaVu);
}
} else {// element is non-null and not an array
buf.append(element.toString());
}
}
if (i == iMax)
break;
buf.append(", ");
}
buf.append(']');
dejaVu.remove(a);
}
示例代码
int[] intValues = { 1, 6, 5, 9, 3, 4, 2, 7, 8 };
//[I@1453ecec
System.out.println(intValues);
//[1, 6, 5, 9, 3, 4, 2, 7, 8]
System.out.println(Arrays.toString(intValues));
int[][] values = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
//[[I@1453ecec, [I@11e78461, [I@76a40575]
System.out.println(Arrays.toString(values));
//[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
System.out.println(Arrays.deepToString(values));
判断数组相等
源代码
/**
* Returns true if the two specified arrays of ints are
* equal to one another.Two arrays are considered equal if both
* arrays contain the same number of elements, and all corresponding pairs
* of elements in the two arrays are equal.In other words, two arrays
* are equal if they contain the same elements in the same order.Also,
* two array references are considered equal if both are null.


*
* @param a one array to be tested for equality
* @param a2 the other array to be tested for equality
* @return true if the two arrays are equal
*/
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;
for (int i=0; i if (a[i] != a2[i])
return false;
return true;
}/**
* Returns true if the two specified arrays are deeply
* equal
to one another.Unlike the {@link #equals(Object[],Object[])}
* method, this method is appropriate for use with nested arrays of
* arbitrary depth.
*
*

Two array references are considered deeply equal if both
* are null, or if they refer to arrays that contain the same
* number of elements and all corresponding pairs of elements in the two
* arrays are deeply equal.
*
*

Two possibly null elements e1 and e2 are
* deeply equal if any of the following conditions hold:
*


    *
  • e1 and e2 are both arrays of object reference
    *types, and Arrays.deepEquals(e1, e2) would return true
    *
  • e1 and e2 are arrays of the same primitive
    *type, and the appropriate overloading of
    *Arrays.equals(e1, e2) would return true.
    *
  • e1 == e2
    *
  • e1.equals(e2) would return true.
    *

* Note that this definition permits null elements at any depth.
*
*

If either of the specified arrays contain themselves as elements
* either directly or indirectly through one or more levels of arrays,
* the behavior of this method is undefined.
*
* @param a1 one array to be tested for equality
* @param a2 the other array to be tested for equality
* @return true if the two arrays are equal
* @see #equals(Object[],Object[])
* @see Objects#deepEquals(Object, Object)
* @since 1.5
*/
public static boolean deepEquals(Object[] a1, Object[] a2) {
if (a1 == a2)
return true;
if (a1 == null || a2==null)
return false;
int length = a1.length;
if (a2.length != length)
return false;
for (int i = 0; i < length; i++) {
Object e1 = a1[i];
Object e2 = a2[i];
if (e1 == e2)
continue;
if (e1 == null)
return false;
// Figure out whether the two elements are equal
boolean eq = deepEquals0(e1, e2);
if (!eq)
return false;
}
return true;
}
static boolean deepEquals0(Object e1, Object e2) {
assert e1 != null;
boolean eq;
if (e1 instanceof Object[] && e2 instanceof Object[])
eq = deepEquals ((Object[]) e1, (Object[]) e2);
else if (e1 instanceof byte[] && e2 instanceof byte[])
eq = equals((byte[]) e1, (byte[]) e2);
else if (e1 instanceof short[] && e2 instanceof short[])
eq = equals((short[]) e1, (short[]) e2);
else if (e1 instanceof int[] && e2 instanceof int[])
eq = equals((int[]) e1, (int[]) e2);
else if (e1 instanceof long[] && e2 instanceof long[])
eq = equals((long[]) e1, (long[]) e2);
else if (e1 instanceof char[] && e2 instanceof char[])
eq = equals((char[]) e1, (char[]) e2);
else if (e1 instanceof float[] && e2 instanceof float[])
eq = equals((float[]) e1, (float[]) e2);
else if (e1 instanceof double[] && e2 instanceof double[])
eq = equals((double[]) e1, (double[]) e2);
else if (e1 instanceof boolean[] && e2 instanceof boolean[])
eq = equals((boolean[]) e1, (boolean[]) e2);
else
eq = e1.equals(e2);
return eq;
}
示例
int[] intValues1 = { 1, 6, 5, 9, 3, 4, 2, 7, 8 };
int[] intValues2 = { 1, 6, 5, 9, 3, 4, 2, 7, 8 };
//true
System.out.println(Arrays.equals(intValues1, intValues2));
int[][] values1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[][] values2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
//true
System.out.println(Arrays.deepEquals(values1, values2));
小结

上述给出了几个使用Arrays工具类的示例,其实也已经有很多的开源工具,如Apache的Common-lang包等。如Apache Common-Lang包中的ArrayUtils工具类就包含了较多的方法, 如:

判断是否为空 /**
*

Checks if an array of primitive ints is empty or null.


*
* @param arraythe array to test
* @return true if the array is empty or null
* @since 2.1
*/
public static boolean isEmpty(int[] array) {
if (array == null || array.length == 0) {
return true;
}
return false;
}判断类型是否一样 /**
*

Checks whether two arrays are the same type taking into account
* multi-dimensional arrays.


*
* @param array1 the first array, must not be null
* @param array2 the second array, must not be null
* @return true if type of arrays matches
* @throws IllegalArgumentException if either array is null
*/
public static boolean isSameType(Object array1, Object array2) {
if (array1 == null || array2 == null) {
throw new IllegalArgumentException("The Array must not be null");
}
return array1.getClass().getName().equals(array2.getClass().getName());
}数组元素反序 /**
*

Reverses the order of the given array.


*
*

There is no special handling for multi-dimensional arrays.


*
*

This method does nothing for a null input array.


*
* @param arraythe array to reverse, may be null
*/
public static void reverse(Object[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
Object tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}

还有其它的一些方法,这里就不一一列举了。ArrayUtils的简单示例如下:


int[] intValues1 = { 1, 6, 5, 9, 3, 4, 2, 7, 8 };
boolean isEmpty = ArrayUtils.isEmpty(intValues1);
//false
System.out.println(isEmpty);
int[] intValues2 = { 1, 6, 5, 9, 3, 4, 2, 7, 8 };
//true
boolean isSameType = ArrayUtils.isSameType(intValues1, intValues2);
System.out.println(isSameType);
//[1, 6, 5, 9, 3, 4, 2, 7, 8]
System.out.println(Arrays.toString(intValues2));
ArrayUtils.reverse(intValues2);
//[8, 7, 2, 4, 3, 9, 5, 6, 1]
System.out.println(Arrays.toString(intValues2));

使用这些工具类可以简化我们对数组的操作,同时简化代码,提高代码可读性。


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台