Java reflection array dynamically created array

Source: Internet
Author: User

Java reflection array dynamically created array

@author Ixenos

Note: Java.lang.reflect.Array is a reflection toolkit, full of static methods, creating arrays based on multidimensional arrays, one-dimensional arrays are only special implementations

Creates a new array with the specified component type and length (one-dimensional array) Newinstance
newinstance (class<?> componenttype,                                 int length)                          throws Negativearraysizeexception
Creates a new array with the specified component type and length. Calling this method is equivalent to creating the following array:
int[] x = {length}; To create an array based on a multidimensional array, a one-dimensional array is only a special implementation of Array.newinstance (ComponentType, x);

Parameters:
componentType - Class object representing the component type of the new array
length -length of the new array
Return:
New array
Thrown:
NullPointerException -If the specified componentType parameter is null
IllegalArgumentException -if ComponentType is Void.TYPE
NegativeArraySizeException -if the specified lengthis negative

Creates a new array with the specified component type and dimension (multidimensional array) Newinstance
newinstance (class<?> componenttype,                                 int ... dimensions)                          throws IllegalArgumentException,                                 Negativearraysizeexception
creates a new array with the specified component type and dimension.
if componentType a non-array class or interface is represented, the new array has dimensions.length dimensions and is componentType used as its component type.
If you componentType represent an array class, the dimensions of the new array are equal to dimensions.length componentType the sum of the dimensions of the number. in this case, the component type of the new array is componentType the component type.

The dimension of the new array cannot exceed the number of array dimensions (typically 255)that the implementation supports.

Parameters:
componentType - Class object representing the component type of the new array
dimensions - int An array representing the new array dimension
Return:
New array
Thrown:
NullPointerException -If the specified componentType argument is null
IllegalArgumentException -If the specified dimensions parameter is an array of 0 dimensions, or the requested number of dimensions exceeds the limit of the number of array dimensions supported by the implementation (typically 2 25), or an array of degrees, or the number of dimensions requested exceeds the limit (usually 225) of the number of array dimensions supported by the implementation, or ComponentType Void.TYPE .   
NegativeArraySizeException -if dimensions any component in the specified parameter is negative.

  To create a multidimensional array example:

get(Object array, int index)returns the value of the index component in the specified Array object

setInt(Object array, int index, int i)sets the value of the index component in the specified array object to the specified int value.

Java reflection array dynamically created array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.