public final class Array extends Object
Array class provides static methods to dynamically create and access Java arrays.
Array permits widening conversions to occur during a get or set operation, but throws an IllegalArgumentException if a narrowing conversion would occur.
| Modifier and Type | Method and Description |
|---|---|
static Object |
get(Object
Returns the value of the indexed component in the specified array object.
|
static boolean |
getBoolean(Object
Returns the value of the indexed component in the specified array object, as a
boolean.
|
static byte |
getByte(Object
Returns the value of the indexed component in the specified array object, as a
byte.
|
static char |
getChar(Object
Returns the value of the indexed component in the specified array object, as a
char.
|
static double |
getDouble(Object
Returns the value of the indexed component in the specified array object, as a
double.
|
static float |
getFloat(Object
Returns the value of the indexed component in the specified array object, as a
float.
|
static int |
getInt(Object
Returns the value of the indexed component in the specified array object, as an
int.
|
static int |
getLength(Object
Returns the length of the specified array object, as an
int.
|
static long |
getLong(Object
Returns the value of the indexed component in the specified array object, as a
long.
|
static short |
getShort(Object
Returns the value of the indexed component in the specified array object, as a
short.
|
static Object |
newInstance(Class
Creates a new array with the specified component type and dimensions.
|
static Object |
newInstance(Class
Creates a new array with the specified component type and length.
|
static void |
set(Object
Sets the value of the indexed component of the specified array object to the specified new value.
|
static void |
setBoolean(Object
Sets the value of the indexed component of the specified array object to the specified
boolean value.
|
static void |
setByte(Object
Sets the value of the indexed component of the specified array object to the specified
byte value.
|
static void |
setChar(Object
Sets the value of the indexed component of the specified array object to the specified
char value.
|
static void |
setDouble(Object
Sets the value of the indexed component of the specified array object to the specified
double value.
|
static void |
setFloat(Object
Sets the value of the indexed component of the specified array object to the specified
float value.
|
static void |
setInt(Object
Sets the value of the indexed component of the specified array object to the specified
int value.
|
static void |
setLong(Object
Sets the value of the indexed component of the specified array object to the specified
long value.
|
static void |
setShort(Object
Sets the value of the indexed component of the specified array object to the specified
short value.
|
public static ObjectnewInstance(Class <?> componentType, int length) throws NegativeArraySizeException
int[] x = {length};
Array.newInstance(componentType, x);
The number of dimensions of the new array must not exceed 255.
componentType - the
Class object representing the component type of the new array
length - the length of the new array
NullPointerException - if the specified
componentType parameter is null
IllegalArgumentException - if componentType is
Void.TYPE or if the number of dimensions of the requested array instance exceed 255.
NegativeArraySizeException - if the specified
length is negative
public static ObjectnewInstance(Class <?> componentType, int... dimensions) throws IllegalArgumentException , NegativeArraySizeException
componentType represents a non-array class or interface, the new array has
dimensions.length dimensions and
componentType as its component type. If
componentType represents an array class, the number of dimensions of the new array is equal to the sum of
dimensions.length and the number of dimensions of
componentType. In this case, the component type of the new array is the component type of
componentType.
The number of dimensions of the new array must not exceed 255.
componentType - the
Class object representing the component type of the new array
dimensions - an array of
int representing the dimensions of the new array
NullPointerException - if the specified
componentType argument is null
IllegalArgumentException - if the specified
dimensions argument is a zero-dimensional array, if componentType is
Void.TYPE , or if the number of dimensions of the requested array instance exceed 255.
NegativeArraySizeException - if any of the components in the specified
dimensions argument is negative.
public static int getLength(Objectarray) throws IllegalArgumentException
int.
array - the array
IllegalArgumentException - if the object argument is not an array
public static Objectget(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
array - the array
index - the index
NullPointerException - If the specified object is null
IllegalArgumentException - If the specified object is not an array
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
public static boolean getBoolean(Objectarray, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
boolean.
array - the array
index - the index
NullPointerException - If the specified object is null
IllegalArgumentException - If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
get(java.lang.Object, int)
public static byte getByte(Objectarray, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
byte.
array - the array
index - the index
NullPointerException - If the specified object is null
IllegalArgumentException - If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
get(java.lang.Object, int)
public static char getChar(Objectarray, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
char.
array - the array
index - the index
NullPointerException - If the specified object is null
IllegalArgumentException - If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
get(java.lang.Object, int)
public static short getShort(Objectarray, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
short.
array - the array
index - the index
NullPointerException - If the specified object is null
IllegalArgumentException - If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
get(java.lang.Object, int)
public static int getInt(Objectarray, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
int.
array - the array
index - the index
NullPointerException - If the specified object is null
IllegalArgumentException - If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
get(java.lang.Object, int)
public static long getLong(Objectarray, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
long.
array - the array
index - the index
NullPointerException - If the specified object is null
IllegalArgumentException - If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
get(java.lang.Object, int)
public static float getFloat(Objectarray, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
float.
array - the array
index - the index
NullPointerException - If the specified object is null
IllegalArgumentException - If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
get(java.lang.Object, int)
public static double getDouble(Objectarray, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
double.
array - the array
index - the index
NullPointerException - If the specified object is null
IllegalArgumentException - If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
get(java.lang.Object, int)
public static void set(Objectarray, int index, Object value) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
array - the array
index - the index into the array
value - the new value of the indexed component
NullPointerException - If the specified object argument is null
IllegalArgumentException - If the specified object argument is not an array, or if the array component type is primitive and an unwrapping conversion fails
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
public static void setBoolean(Objectarray, int index, boolean z) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
boolean value.
array - the array
index - the index into the array
z - the new value of the indexed component
NullPointerException - If the specified object argument is null
IllegalArgumentException - If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
set(java.lang.Object, int, java.lang.Object)
public static void setByte(Objectarray, int index, byte b) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
byte value.
array - the array
index - the index into the array
b - the new value of the indexed component
NullPointerException - If the specified object argument is null
IllegalArgumentException - If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
set(java.lang.Object, int, java.lang.Object)
public static void setChar(Objectarray, int index, char c) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
char value.
array - the array
index - the index into the array
c - the new value of the indexed component
NullPointerException - If the specified object argument is null
IllegalArgumentException - If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
set(java.lang.Object, int, java.lang.Object)
public static void setShort(Objectarray, int index, short s) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
short value.
array - the array
index - the index into the array
s - the new value of the indexed component
NullPointerException - If the specified object argument is null
IllegalArgumentException - If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
set(java.lang.Object, int, java.lang.Object)
public static void setInt(Objectarray, int index, int i) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
int value.
array - the array
index - the index into the array
i - the new value of the indexed component
NullPointerException - If the specified object argument is null
IllegalArgumentException - If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
set(java.lang.Object, int, java.lang.Object)
public static void setLong(Objectarray, int index, long l) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
long value.
array - the array
index - the index into the array
l - the new value of the indexed component
NullPointerException - If the specified object argument is null
IllegalArgumentException - If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
set(java.lang.Object, int, java.lang.Object)
public static void setFloat(Objectarray, int index, float f) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
float value.
array - the array
index - the index into the array
f - the new value of the indexed component
NullPointerException - If the specified object argument is null
IllegalArgumentException - If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
set(java.lang.Object, int, java.lang.Object)
public static void setDouble(Objectarray, int index, double d) throws IllegalArgumentException , ArrayIndexOutOfBoundsException
double value.
array - the array
index - the index into the array
d - the new value of the indexed component
NullPointerException - If the specified object argument is null
IllegalArgumentException - If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
ArrayIndexOutOfBoundsException - If the specified
index argument is negative, or if it is greater than or equal to the length of the specified array
set(java.lang.Object, int, java.lang.Object)