/**
* Returns a fixed-size list backed bythe specified array. (Changes to
* the returned list"write through"tothe 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}.
*
* <p>This method also provides a convenient way to create a fixed-size
* list initialized tocontain several elements:
* <pre>
* List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
* </pre>
*
* @param <T> theclassofthe objects inthe array
* @param a the array by which thelist will be backed
* @return a list view ofthe specified array
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
/**
* 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 <tt>null</tt>.
* Such indices will exist ifand only if the specified length
* is greater than that of the original array.
* The resulting array isof exactly the same classastheoriginalarray.
*
* @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 <tt>newLength</tt> is negative
* @throws NullPointerException if <tt>original</tt> isnull
* @since1.6
*/
public static <T> 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 <tt>null</tt>.
* Such indices will exist ifand only if the specified length
* is greater than that of the original array.
* The resulting array isof the class <tt>newType</tt>.
*
* @param original the array to be copied
* @param newLength the length of the copy to be returned
* @param newType the classofthecopytobereturned
* @return a copy of the original array, truncated or padded with nulls
* to obtain the specified length
* @throws NegativeArraySizeException if <tt>newLength</tt> is negative
* @throws NullPointerException if <tt>original</tt> isnull
* @throws ArrayStoreException if an element copied from
* <tt>original</tt> isnotof a runtime type that can be stored in
* an array ofclass <tt>newType</tt>
* @since1.6
*/
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
// 判断新的数据类型是否是Object[]
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[])
// 若不是Object[]类型重新生成一个新的数组,类型为T Array.newInstance(newType.getComponentType(), newLength);
// 调用System 中的Native 方法进行数组copy
// 从 original 的 0 位置开始,向 copy 的 0 位置,复制元素,长度为 新旧数组的最小长度
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}