public final class ArrayUtil extends Object
getElement(Object, int),得到数组中的某个元素newArray(Class, int),构造一个指定类型以及指定长度的数组.
ArrayUtils.contains(boolean[], boolean)ArrayUtils.contains(byte[], byte)ArrayUtils.contains(char[], char)ArrayUtils.contains(double[], double)ArrayUtils.contains(float[], float)ArrayUtils.contains(int[], int)ArrayUtils.contains(long[], long)ArrayUtils.contains(Object[], Object)ArrayUtils.contains(short[], short)ArrayUtils.contains(double[], double, double)
- 如果你想detect所有的 array类型,你必须判断一个object is an instanceof boolean[], byte[], short[], char[], int[], long[], float[], double[], or Object[]
- Object[] 数组 Integer/String...自定义的对象User.等数组也 instanceof Object[],二维数组不管是primitive 还是包装类型,都instanceof Object[];
so depending on how you want to handle nested arrays, it can get complicated.
ArrayUtils| Modifier and Type | Method and Description |
|---|---|
static <T> T |
getElement(Object array,
int index)
得到指定数组对象
array 中指定索引 index 的元素. |
static <T> T[] |
newArray(Class<T> componentType,
int length)
构造一个指定类型
componentType 以及指定长度 length的数组. |
public static <T> T getElement(Object array, int index)
array 中指定索引 index 的元素.
- 支持 primitive type类型数组
- Returns the value of the indexed component in the specified array object.
The value is automatically wrapped in an object if it has a primitive type.
ArrayUtil.getElement(new String[] { "jinxin", "feilong", "1" }, 2) = 1
ArrayUtil.getElement(new int[] { 5, 8, 2, 0 }, 2) = 2
T - the generic typearray - 数组index - 索引,从0开始index是负数,或者大于等于指定数组 array 的长度,抛出 ArrayIndexOutOfBoundsExceptionNullPointerException - 如果 array 是nullIllegalArgumentException - 如果 array 不是数组Array.get(Object, int)public static <T> T[] newArray(Class<T> componentType, int length)
componentType 以及指定长度 length的数组.
assertArrayEquals(new Integer[] {}, ArrayUtil.newArray(Integer.class, 0));
assertArrayEquals(new Integer[] { null, null, null }, ArrayUtil.newArray(Integer.class, 3));
T - the generic typecomponentType - 数组的类型length - 数组的长度componentType 是null,抛出 NullPointerExceptionlength < 0 ,抛出 IllegalArgumentExceptionArray.newInstance(Class, int),
Array.newInstance(Class, int...),
"com.google.common.collect#newArray(Class, int)"Copyright © 2008-2019 by feilong