Reflection Array Class in Java - GeeksforGeeks

10 Nov, 2021
The Array class in java.lang.reflect package is a part of the Java Reflection. This class provides static methods to create and access Java arrays dynamically. It is a final class, which means it can’t be instantiated or changed. Only the methods of this class can be used by the class name itself.
The java.util.Arrays class contains various methods for manipulating arrays (such as sorting and searching), whereas this java.lang.reflect.Array class provides static methods to create and access Java arrays dynamically. This Array class keeps the array to be type-safe.
java.lang.Object
 ↳ java.lang.reflect.Array
public final class Array extends Object
Array.<function name>;
Creating an array using reflect.Array Class is different from the usual way. The process to create such an array is as follows: 
Get the size of the array to be created
To create an array (say of X class), use the newInstance() method of Array class to pass the X class as the type of the array, and the size of the array, as parameters.
Syntax: 
X[] arrayOfXType = (X[]) Array.newInstance(X.class, size);
Where X is to be replaced by the type of the array like int, double, etc.
This method returns an Object array of the given size, then cast into the required X[] type.
Hence the required array of type X has been created.
Below is an example to create an integer array of size 5, using the Array class:
Example: To create an integer array of size 5: 
Java
// Java code to create an integer array of size 5,// using the Array class: import java.lang.reflect.Array;import java.util.Arrays; public class GfG {    public static void main(String[] args)    {         // Get the size of the array        int sizeOfArray = 5;         // Create an integer array        // using reflect.Array class        // This is done using the newInstance() method         int[] intArray = (int[])Array.newInstance(            int.class, sizeOfArray);               // Printing the Array content        System.out.println(Arrays.toString(intArray));    }}
[0, 0, 0, 0, 0]
Like creating an array, adding elements in the array using reflect.Array Class is also different from the usual way. The process to add elements in such an array is as follows: 
Get the value of the element to be added.
Get the index at which the element is to be added.
To add an element in an array (say of X class), use the setX() method of Array class, where X is to be replaced by the type of the array such as setInt(), setDouble(), etc. This method takes the X[] as the first parameter, the index at which the element is added as the second parameter, and the element as the third parameter in the below syntax.
Syntax: 
Array.setX(X[], indexOfInsertion, elementToBeInserted);
Where X is to be replaced by the type of the array like int, double, etc.
This method inserts the element at the specified index in this array.
Hence the required element has been inserted into the array.
Below is an example to add an element into an integer array, using the Array class:
Example: To add an element into an integer array: 
Java
// Java code to add an element into an integer array,// using the Array class: import java.lang.reflect.Array;import java.util.Arrays; public class GfG {    public static void main(String[] args)    {         // Get the size of the array        int sizeOfArray = 3;         // Create an integer array        // using reflect.Array class        // This is done using the newInstance() method        int[] intArray = (int[])Array.newInstance(            int.class, sizeOfArray);         // Add elements into the array        // This is done using the setInt() method        Array.setInt(intArray, 0, 10);        Array.setInt(intArray, 1, 20);        Array.setInt(intArray, 2, 30);         // Printing the Array content        System.out.println(Arrays.toString(intArray));    }}
[10, 20, 30]
Like creating an array, retrieving elements in the array using reflect.Array Class is also different from the usual way. The process to retrieve elements in such an array is as follows: 
Get the index at which the element is to be retrieved.
To retrieve an element in an array (say of X class), use the getX() method of Array class, where X is to be replaced by the type of the array such as getInt(), getDouble(), etc. This method takes the X[] as the first parameter and the index at which the element is retrieved as the second parameter in the syntax below.
Syntax: 
Array.getX(X[], indexOfRetrieval);
Where X is to be replaced by the type of the array like int, double, etc.
This method retrieves the element at the specified index from this array.
Hence the required element has been retrieved from the array.
Below is an example to retrieve an element from an integer array using the Array class:
Example: To retrieve an element from an integer array: 
Java
// Java code to retrieve an element from an integer array,// using the Array class: import java.lang.reflect.Array;import java.util.Arrays; public class GfG {    public static void main(String[] args)    {         // Get the size of the array        int sizeOfArray = 3;         // Create an integer array        // using reflect.Array class        // This is done using the newInstance() method        int[] intArray = (int[])Array.newInstance(            int.class, sizeOfArray);         // Add elements from the array        // This is done using the getInt() method        Array.setInt(intArray, 0, 10);        Array.setInt(intArray, 1, 20);        Array.setInt(intArray, 2, 30);         // Printing the Array content        System.out.println(Arrays.toString(intArray));         // Retrieve elements from the array        // This is done using the getInt() method        System.out.println("Element at index 0: "                           + Array.getInt(intArray, 0));        System.out.println("Element at index 1: "                           + Array.getInt(intArray, 1));        System.out.println("Element at index 2: "                           + Array.getInt(intArray, 2));    }}
[10, 20, 30]
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
nishkarshgandhi
Java-Class and Object
java-reflection-array
Java
Java-Class and Object
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Interfaces in Java
Initialize an ArrayList in Java
ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Singleton Class in Java
LinkedList in Java