版本
JDK8(JDK1.8)
Array final类(无法被继承)源码重点
1.Array类提供静态方法来动态创建和访问Java数组。
2.Array允许在get或set操作期间进行加宽转换,但如果发生缩小转换,则抛出IllegalArgumentException,这里的加宽指的是字节范围加宽,如int->double是合法的,而不是指数组长度加宽
3.Array类内部有两个私有原生方法用于创建数组,newArray和multiNewArray, 分别对应创建一维数组和创建多维数组
4.Array类的部分方法
| 方法名 | 作用 |
|---|---|
| Object newInstance(Class> componentType, int length) | 创建特定类型,长度为length的一维数组,底层调用newArray(.)方法 |
| Object newInstance(Class> componentType, int… dimensions) | 创建特定类型的多维数组,底层调用multiNewArray(.)方法 |
| Object newArray(Class> componentType, int length) | 创建长度为length的一维数组 |
| Object multiNewArray(Class> componentType, int[] dimensions) | 根据dimensions数组中的值创建特定维度长度的多维数组 |
| int getLength(Object array) | 以int形式返回指定数组对象的长度 |
| Object get(Object array, int index) | 返回指定数组对象中索引元素的值 |
| boolean getBoolean(Object array, int index) | 以boolean形式返回指定数组对象中索引元素的值 |
| byte getByte(Object array, int index) | 以byte的形式返回指定数组对象中索引组件的值 |
| char getChar(Object array, int index) | 以char的形式返回指定数组对象中索引组件的值 |
| short getShort(Object array, int index) | 以short的形式返回指定数组对象中索引组件的值 |
| int getInt(Object array, int index) | 以int的形式返回指定数组对象中索引组件的值 |
| long getLong(Object array, int index) | 以long的形式返回指定数组对象中索引组件的值 |
| float getFloat(Object array, int index) | 以float的形式返回指定数组对象中索引组件的值 |
| double getDouble(Object array, int index) | 以double的形式返回指定数组对象中索引组件的值 |
| void set(Object array, int index, Object value) | 将指定数组对象的索引组件的值设置为指定的新值 |
| void setBoolean(Object array, int index, boolean z) | 将指定数组对象的索引组件的值设置为指定的boolean值 |
| void setByte(Object array, int index, byte b) | 将指定数组对象的索引组件的值设置为指定的byte值 |
| void setChar(Object array, int index, char c) | 将指定数组对象的索引组件的值设置为指定的char值 |
| void setShort(Object array, int index, short s) | 将指定数组对象的索引组件的值设置为指定的short值 |
| void setInt(Object array, int index, int i) | 将指定数组对象的索引组件的值设置为指定的int值 |
| void setLong(Object array, int index, long l) | 将指定数组对象的索引组件的值设置为指定的long值 |
| void setFloat(Object array, int index, float f) | 将指定数组对象的索引组件的值设置为指定的float值 |
| void setDouble(Object array, int index, double d) | 将指定数组对象的索引组件的值设置为指定的double值 |
Array final类源码
package java.lang.reflect;
import jdk.internal.HotSpotIntrinsicCandidate;
public final
class Array {
private Array() {}
public static Object newInstance(Class> componentType, int length)
throws NegativeArraySizeException {
return newArray(componentType, length);
}
public static Object newInstance(Class> componentType, int... dimensions)
throws IllegalArgumentException, NegativeArraySizeException {
return multiNewArray(componentType, dimensions);
}
@HotSpotIntrinsicCandidate
public static native int getLength(Object array)
throws IllegalArgumentException;
public static native Object get(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native boolean getBoolean(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native byte getByte(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native char getChar(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native short getShort(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native int getInt(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native long getLong(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native float getFloat(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native double getDouble(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void set(Object array, int index, Object value)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setBoolean(Object array, int index, boolean z)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setByte(Object array, int index, byte b)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setChar(Object array, int index, char c)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setShort(Object array, int index, short s)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setInt(Object array, int index, int i)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setLong(Object array, int index, long l)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setFloat(Object array, int index, float f)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setDouble(Object array, int index, double d)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
// 创建一维数组
@HotSpotIntrinsicCandidate
private static native Object newArray(Class> componentType, int length)
throws NegativeArraySizeException;
// 创建多维数组
private static native Object multiNewArray(Class> componentType,
int[] dimensions)
throws IllegalArgumentException, NegativeArraySizeException;
}



