public final class Array extends Object
IllegalArgumentException
if you attempt
a narrowing conversion. Also, when accessing primitive arrays, this
class performs object wrapping and unwrapping as necessary.
Note: This class returns and accepts types as Classes, even
primitive types; there are Class types defined that represent each
different primitive type. They are java.lang.Boolean.TYPE,
java.lang.Byte.TYPE,
, also available as boolean.class,
byte.class
, etc. These are not to be confused with the
classes java.lang.Boolean, java.lang.Byte
, etc., which are
real classes. Note also that the shorthand Object[].class
is a convenient way to get array Classes.
Performance note: This class performs best when it does not have to convert primitive types. The further along the chain it has to convert, the worse performance will be. You're best off using the array as whatever type it already is, and then converting the result. You will do even worse if you do this and use the generic set() function.
Boolean.TYPE
,
Byte.TYPE
,
Short.TYPE
,
Character.TYPE
,
Integer.TYPE
,
Long.TYPE
,
Float.TYPE
,
Double.TYPE
Modifier and Type | Method and Description |
---|---|
static Object |
get(Object array,
int index)
Gets an element of an array.
|
static boolean |
getBoolean(Object array,
int index)
Gets an element of a boolean array.
|
static byte |
getByte(Object array,
int index)
Gets an element of a byte array.
|
static char |
getChar(Object array,
int index)
Gets an element of a char array.
|
static double |
getDouble(Object array,
int index)
Gets an element of a double array.
|
static float |
getFloat(Object array,
int index)
Gets an element of a float array.
|
static int |
getInt(Object array,
int index)
Gets an element of an int array.
|
static int |
getLength(Object array)
Gets the array length.
|
static long |
getLong(Object array,
int index)
Gets an element of a long array.
|
static short |
getShort(Object array,
int index)
Gets an element of a short array.
|
static Object |
newInstance(Class<?> componentType,
int length)
Creates a new single-dimensioned array.
|
static Object |
newInstance(Class<?> elementType,
int[] dimensions)
Creates a new multi-dimensioned array.
|
static void |
set(Object array,
int index,
Object value)
Sets an element of an array.
|
static void |
setBoolean(Object array,
int index,
boolean value)
Sets an element of a boolean array.
|
static void |
setByte(Object array,
int index,
byte value)
Sets an element of a byte array.
|
static void |
setChar(Object array,
int index,
char value)
Sets an element of a char array.
|
static void |
setDouble(Object array,
int index,
double value)
Sets an element of a double array.
|
static void |
setFloat(Object array,
int index,
float value)
Sets an element of a float array.
|
static void |
setInt(Object array,
int index,
int value)
Sets an element of an int array.
|
static void |
setLong(Object array,
int index,
long value)
Sets an element of a long array.
|
static void |
setShort(Object array,
int index,
short value)
Sets an element of a short array.
|
public static Object newInstance(Class<?> componentType, int length)
componentType
- the type of the array to createlength
- the length of the array to createNullPointerException
- if componentType
is nullIllegalArgumentException
- if componentType
is
Void.TYPE
NegativeArraySizeException
- when length is less than 0OutOfMemoryError
- if memory allocation failspublic static Object newInstance(Class<?> elementType, int[] dimensions)
componentType
- the type of the array to create.dimensions
- the dimensions of the array to create. Each element
in dimensions
makes another dimension of the new
array. Thus, Array.newInstance(java.lang.Boolean,
new int[]{1,2,3})
is the same as
new java.lang.Boolean[1][2][3]
NullPointerException
- if componentType or dimension is nullIllegalArgumentException
- if the the size of
dimensions
is 0 or exceeds the maximum number of
array dimensions in the VM; or if componentType is Void.TYPENegativeArraySizeException
- when any of the dimensions is less
than 0OutOfMemoryError
- if memory allocation failspublic static int getLength(Object array)
array
- the arrayIllegalArgumentException
- if array
is not an arrayNullPointerException
- if array
is nullpublic static Object get(Object array, int index)
array
- the array to accessindex
- the array index to accessarray[index]
IllegalArgumentException
- if array
is not an arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsgetBoolean(Object, int)
,
getByte(Object, int)
,
getChar(Object, int)
,
getShort(Object, int)
,
getInt(Object, int)
,
getLong(Object, int)
,
getFloat(Object, int)
,
getDouble(Object, int)
public static boolean getBoolean(Object array, int index)
array
- the array to accessindex
- the array index to accessarray[index]
IllegalArgumentException
- if array
is not a boolean
arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsget(Object, int)
public static byte getByte(Object array, int index)
array
- the array to accessindex
- the array index to accessarray[index]
IllegalArgumentException
- if array
is not a byte
arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsget(Object, int)
public static char getChar(Object array, int index)
array
- the array to accessindex
- the array index to accessarray[index]
IllegalArgumentException
- if array
is not a char
arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsget(Object, int)
public static short getShort(Object array, int index)
array
- the array to accessindex
- the array index to accessarray[index]
IllegalArgumentException
- if array
is not a byte
or char arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsget(Object, int)
public static int getInt(Object array, int index)
array
- the array to accessindex
- the array index to accessarray[index]
IllegalArgumentException
- if array
is not a byte,
char, short, or int arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsget(Object, int)
public static long getLong(Object array, int index)
array
- the array to accessindex
- the array index to accessarray[index]
IllegalArgumentException
- if array
is not a byte,
char, short, int, or long arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsget(Object, int)
public static float getFloat(Object array, int index)
array
- the array to accessindex
- the array index to accessarray[index]
IllegalArgumentException
- if array
is not a byte,
char, short, int, long, or float arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsget(Object, int)
public static double getDouble(Object array, int index)
array
- the array to accessindex
- the array index to accessarray[index]
IllegalArgumentException
- if array
is not a byte,
char, short, int, long, float, or double arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsget(Object, int)
public static void set(Object array, int index, Object value)
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to setIllegalArgumentException
- if array
is not an array,
or the array is primitive and unwrapping value fails, or the
value is not assignable to the array component typeNullPointerException
- if array is null, or if array is primitive
and value is nullArrayIndexOutOfBoundsException
- if index
is out of
boundssetBoolean(Object, int, boolean)
,
setByte(Object, int, byte)
,
setChar(Object, int, char)
,
setShort(Object, int, short)
,
setInt(Object, int, int)
,
setLong(Object, int, long)
,
setFloat(Object, int, float)
,
setDouble(Object, int, double)
public static void setBoolean(Object array, int index, boolean value)
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to setIllegalArgumentException
- if array
is not a boolean
arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsset(Object, int, Object)
public static void setByte(Object array, int index, byte value)
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to setIllegalArgumentException
- if array
is not a byte,
short, int, long, float, or double arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsset(Object, int, Object)
public static void setChar(Object array, int index, char value)
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to setIllegalArgumentException
- if array
is not a char,
int, long, float, or double arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsset(Object, int, Object)
public static void setShort(Object array, int index, short value)
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to setIllegalArgumentException
- if array
is not a short,
int, long, float, or double arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsset(Object, int, Object)
public static void setInt(Object array, int index, int value)
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to setIllegalArgumentException
- if array
is not an int,
long, float, or double arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsset(Object, int, Object)
public static void setLong(Object array, int index, long value)
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to setIllegalArgumentException
- if array
is not a long,
float, or double arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsset(Object, int, Object)
public static void setFloat(Object array, int index, float value)
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to setIllegalArgumentException
- if array
is not a float
or double arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsset(Object, int, Object)
public static void setDouble(Object array, int index, double value)
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to setIllegalArgumentException
- if array
is not a double
arrayNullPointerException
- if array
is nullArrayIndexOutOfBoundsException
- if index
is out of
boundsset(Object, int, Object)