java.lang.reflect
Class Array
Array holds static helper functions that allow you to create and
manipulate arrays by reflection. Operations know how to perform widening
conversions, but throw
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.
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> componentType, 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.
|
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
get
public static Object get(Object array,
int index)
Gets an element of an array. Primitive elements will be wrapped in
the corresponding class type.
array
- the array to accessindex
- the array index to access
- the element at
array[index]
getBoolean(Object,int)
, getByte(Object,int)
, getChar(Object,int)
, getShort(Object,int)
, getInt(Object,int)
, getLong(Object,int)
, getFloat(Object,int)
, getDouble(Object,int)
getBoolean
public static boolean getBoolean(Object array,
int index)
Gets an element of a boolean array.
array
- the array to accessindex
- the array index to access
- the boolean element at
array[index]
getByte
public static byte getByte(Object array,
int index)
Gets an element of a byte array.
array
- the array to accessindex
- the array index to access
- the byte element at
array[index]
getChar
public static char getChar(Object array,
int index)
Gets an element of a char array.
array
- the array to accessindex
- the array index to access
- the char element at
array[index]
getDouble
public static double getDouble(Object array,
int index)
Gets an element of a double array.
array
- the array to accessindex
- the array index to access
- the double element at
array[index]
getFloat
public static float getFloat(Object array,
int index)
Gets an element of a float array.
array
- the array to accessindex
- the array index to access
- the float element at
array[index]
getInt
public static int getInt(Object array,
int index)
Gets an element of an int array.
array
- the array to accessindex
- the array index to access
- the int element at
array[index]
getLong
public static long getLong(Object array,
int index)
Gets an element of a long array.
array
- the array to accessindex
- the array index to access
- the long element at
array[index]
getShort
public static short getShort(Object array,
int index)
Gets an element of a short array.
array
- the array to accessindex
- the array index to access
- the short element at
array[index]
newInstance
public static Object newInstance(Class> componentType,
int length)
Creates a new single-dimensioned array.
componentType
- the type of the array to createlength
- the length of the array to create
- the created array, cast to an Object
newInstance
public static Object newInstance(Class> componentType,
int[] dimensions)
Creates a new multi-dimensioned array. The new array has the same
component type as the argument class, and the number of dimensions
in the new array is the sum of the dimensions of the argument class
and the length of the argument dimensions. Virtual Machine limitations
forbid too many dimensions (usually 255 is the maximum); but even
50 dimensions of 2 elements in each dimension would exceed your memory
long beforehand!
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]
- the created array, cast to an Object
set
public static void set(Object array,
int index,
Object value)
Sets an element of an array. If the array is primitive, then the new
value is unwrapped and widened.
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to set
setBoolean(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)
setBoolean
public static void setBoolean(Object array,
int index,
boolean value)
Sets an element of a boolean array.
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to set
setByte
public static void setByte(Object array,
int index,
byte value)
Sets an element of a byte array.
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to set
setChar
public static void setChar(Object array,
int index,
char value)
Sets an element of a char array.
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to set
setDouble
public static void setDouble(Object array,
int index,
double value)
Sets an element of a double array.
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to set
setFloat
public static void setFloat(Object array,
int index,
float value)
Sets an element of a float array.
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to set
setInt
public static void setInt(Object array,
int index,
int value)
Sets an element of an int array.
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to set
setLong
public static void setLong(Object array,
int index,
long value)
Sets an element of a long array.
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to set
setShort
public static void setShort(Object array,
int index,
short value)
Sets an element of a short array.
array
- the array to set a value ofindex
- the array index to set the value tovalue
- the value to set
java.lang.reflect.Array - manipulate arrays by reflection
Copyright (C) 1998, 1999, 2001, 2003, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.