Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 327
نمايش فراداده

Arrayjava.lang.reflect

Java 1.1

This class contains methods that allow you to set and query the values of array elements, to determine the length of an array, and to create new instances of arrays. Note that the Array class can manipulate only array values, not array types; Java data types, including array types, are represented by java.lang.Class. Since the Array class represents a Java value, unlike the Field, Method, and Constructor classes, which represent class members, the Array class is significantly different (despite some surface similarities) from those other classes in this package. Most notably, all the methods of Array are static and apply to all array values, not just a specific field, method, or constructor.

The get( ) method returns the value of the specified element of the specified array as an Object. If the array elements are of a primitive type, the value is converted to a wrapper object before being returned. You can also use getInt( ) and related methods to query array elements and return them as specific primitive types. The set( ) method and its primitive type variants perform the opposite operation. Also, the getLength( ) method returns the length of the array.

The newInstance( ) methods create new arrays. One version of this method is passed the number of elements in the array and the type of those elements. The other version of this method creates multidimensional arrays. Besides specifying the component type of the array, it is passed an array of numbers. The length of this array specifies the number of dimensions for the array to be created, and the values of each of the array elements specify the size of each dimension of the created array.

public final class

Array { // No Constructor // Public Class Methods public static Object

get (Object

array , int

index ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static boolean

getBoolean (Object

array , int

index ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static byte

getByte (Object

array , int

index ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static char

getChar (Object

array , int

index ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static double

getDouble (Object

array , int

index ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static float

getFloat (Object

array , int

index ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static int

getInt (Object

array , int

index ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static int

getLength (Object

array ) throws IllegalArgumentException; native public static long

getLong (Object

array , int

index ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static short

getShort (Object

array , int

index ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static Object

newInstance (Class<?>

componentType , int

length ) throws NegativeArraySizeException; public static Object

newInstance (Class<?>

componentType , int[ ]

dimensions ) throws IllegalArgumentException, NegativeArraySizeException; public static void

set (Object

array , int

index , Object

value ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static void

setBoolean (Object

array , int

index , boolean

z ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static void

setByte (Object

array , int

index , byte

b ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static void

setChar (Object

array , int

index , char

c ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static void

setDouble (Object

array , int

index , double

d ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static void

setFloat (Object

array , int

index , float

f ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static void

setInt (Object

array , int

index , int

i ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static void

setLong (Object

array , int

index , long

l ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native public static void

setShort (Object

array , int

index , short

s ) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; native }