Array
Stay organized with collections
Save and categorize content based on your preferences.
The 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.
Public Method Summary
static
Object
|
get( Object array, int index)
Returns the value of the indexed component in the specified
array object.
|
static
boolean
|
getBoolean( Object array, int index)
Returns the value of the indexed component in the specified
array object, as a boolean .
|
static
byte
|
getByte( Object array, int index)
Returns the value of the indexed component in the specified
array object, as a byte .
|
static
char
|
getChar( Object array, int index)
Returns the value of the indexed component in the specified
array object, as a char .
|
static
double
|
getDouble( Object array, int index)
Returns the value of the indexed component in the specified
array object, as a double .
|
static
float
|
getFloat( Object array, int index)
Returns the value of the indexed component in the specified
array object, as a float .
|
static
int
|
getInt( Object array, int index)
Returns the value of the indexed component in the specified
array object, as an int .
|
static
int
|
getLength( Object array)
Returns the length of the specified array object, as an int .
|
static
long
|
getLong( Object array, int index)
Returns the value of the indexed component in the specified
array object, as a long .
|
static
short
|
getShort( Object array, int index)
Returns the value of the indexed component in the specified
array object, as a short .
|
static
Object
|
newInstance( Class<?> componentType, int length)
Creates a new array with the specified component type and
length.
|
static
Object
|
newInstance( Class<?> componentType, int... dimensions)
Creates a new array
with the specified component type and dimensions.
|
static
void
|
set( Object array, int index, Object value)
Sets the value of the indexed component of the specified array
object to the specified new value.
|
static
void
|
setBoolean( Object array, int index, boolean value)
Sets the value of the indexed component of the specified array
object to the specified boolean value.
|
static
void
|
setByte( Object array, int index, byte value)
Sets the value of the indexed component of the specified array
object to the specified byte value.
|
static
void
|
setChar( Object array, int index, char value)
Sets the value of the indexed component of the specified array
object to the specified char value.
|
static
void
|
setDouble( Object array, int index, double value)
Sets the value of the indexed component of the specified array
object to the specified double value.
|
static
void
|
setFloat( Object array, int index, float value)
Sets the value of the indexed component of the specified array
object to the specified float value.
|
static
void
|
setInt( Object array, int index, int value)
Sets the value of the indexed component of the specified array
object to the specified int value.
|
static
void
|
setLong( Object array, int index, long value)
Sets the value of the indexed component of the specified array
object to the specified long value.
|
static
void
|
setShort( Object array, int index, short value)
Sets the value of the indexed component of the specified array
object to the specified short value.
|
Inherited Method Summary
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals( Object obj)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long timeout, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long timeout)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
Public Methods
public
static
Object
get
(Object array, int index)
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.
Parameters
array |
the array |
index |
the index |
Returns
- the (possibly wrapped) value of the indexed component in
the specified array
public
static
boolean
getBoolean
(Object array, int index)
Returns the value of the indexed component in the specified
array object, as a boolean
.
Parameters
array |
the array |
index |
the index |
Returns
- the value of the indexed component in the specified array
public
static
byte
getByte
(Object array, int index)
Returns the value of the indexed component in the specified
array object, as a byte
.
Parameters
array |
the array |
index |
the index |
Returns
- the value of the indexed component in the specified array
public
static
char
getChar
(Object array, int index)
Returns the value of the indexed component in the specified
array object, as a char
.
Parameters
array |
the array |
index |
the index |
Returns
- the value of the indexed component in the specified array
public
static
double
getDouble
(Object array, int index)
Returns the value of the indexed component in the specified
array object, as a double
.
Parameters
array |
the array |
index |
the index |
Returns
- the value of the indexed component in the specified array
public
static
float
getFloat
(Object array, int index)
Returns the value of the indexed component in the specified
array object, as a float
.
Parameters
array |
the array |
index |
the index |
Returns
- the value of the indexed component in the specified array
public
static
int
getInt
(Object array, int index)
Returns the value of the indexed component in the specified
array object, as an int
.
Parameters
array |
the array |
index |
the index |
Returns
- the value of the indexed component in the specified array
public
static
int
getLength
(Object array)
Returns the length of the specified array object, as an int
.
public
static
long
getLong
(Object array, int index)
Returns the value of the indexed component in the specified
array object, as a long
.
Parameters
array |
the array |
index |
the index |
Returns
- the value of the indexed component in the specified array
public
static
short
getShort
(Object array, int index)
Returns the value of the indexed component in the specified
array object, as a short
.
Parameters
array |
the array |
index |
the index |
Returns
- the value of the indexed component in the specified array
public
static
Object
newInstance
(Class<?> componentType, int length)
Creates a new array with the specified component type and
length.
Invoking this method is equivalent to creating an array
as follows:
int[] x = {length};
Array.newInstance(componentType, x);
The number of dimensions of the new array must not
exceed 255.
Parameters
componentType |
the Class object representing the
component type of the new array |
length |
the length of the new array |
public
static
Object
newInstance
(Class<?> componentType, int... dimensions)
Creates a new array
with the specified component type and dimensions.
If 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.
Parameters
componentType |
the Class object representing the component
type of the new array |
dimensions |
an array of int representing the dimensions of
the new array |
public
static
void
set
(Object array, int index, Object value)
Sets the value of the indexed component of the specified array
object to the specified new value. The new value is first
automatically unwrapped if the array has a primitive component
type.
Parameters
array |
the array |
index |
the index into the array |
value |
the new value of the indexed component |
public
static
void
setBoolean
(Object array, int index, boolean value)
Sets the value of the indexed component of the specified array
object to the specified boolean
value.
Parameters
array |
the array |
index |
the index into the array |
value |
the new value of the indexed component |
Throws
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 |
public
static
void
setByte
(Object array, int index, byte value)
Sets the value of the indexed component of the specified array
object to the specified byte
value.
Parameters
array |
the array |
index |
the index into the array |
value |
the new value of the indexed component |
Throws
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 |
public
static
void
setChar
(Object array, int index, char value)
Sets the value of the indexed component of the specified array
object to the specified char
value.
Parameters
array |
the array |
index |
the index into the array |
value |
the new value of the indexed component |
Throws
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 |
public
static
void
setDouble
(Object array, int index, double value)
Sets the value of the indexed component of the specified array
object to the specified double
value.
Parameters
array |
the array |
index |
the index into the array |
value |
the new value of the indexed component |
Throws
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 |
public
static
void
setFloat
(Object array, int index, float value)
Sets the value of the indexed component of the specified array
object to the specified float
value.
Parameters
array |
the array |
index |
the index into the array |
value |
the new value of the indexed component |
Throws
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 |
public
static
void
setInt
(Object array, int index, int value)
Sets the value of the indexed component of the specified array
object to the specified int
value.
Parameters
array |
the array |
index |
the index into the array |
value |
the new value of the indexed component |
Throws
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 |
public
static
void
setLong
(Object array, int index, long value)
Sets the value of the indexed component of the specified array
object to the specified long
value.
Parameters
array |
the array |
index |
the index into the array |
value |
the new value of the indexed component |
Throws
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 |
public
static
void
setShort
(Object array, int index, short value)
Sets the value of the indexed component of the specified array
object to the specified short
value.
Parameters
array |
the array |
index |
the index into the array |
value |
the new value of the indexed component |
Throws
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 |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["The Java `Array` class provides static methods for dynamic array creation and manipulation, including methods for creating, accessing, and modifying arrays."],["It supports widening conversions during get/set operations but throws an `IllegalArgumentException` for narrowing conversions."],["The `Array` class is primarily used for reflection, operating on `Object` instances representing arrays, and is less common for standard array operations."],["Exceptions like `NullPointerException`, `IllegalArgumentException`, and `ArrayIndexOutOfBoundsException` are thrown for invalid arguments or array access issues."],["Direct array creation (e.g., `int[] myArray = new int[5]`) is generally preferred for non-reflective scenarios, while `Array` class methods are useful for reflection-based tasks."]]],["The `Array` class in Java enables dynamic array handling, supporting runtime creation and manipulation. It allows retrieving and setting array element values with methods like `get`, `getBoolean`, `set`, and `setBoolean`, providing type-specific versions for primitives. The class also supports array creation using `newInstance`, determining array length with `getLength`, implicit type widening, and error handling by throwing exceptions such as `NullPointerException`, `IllegalArgumentException`, and `ArrayIndexOutOfBoundsException` for invalid array access.\n"]]