Class Declaration
The syntax of the Array class is shown below:
public final class Array extends Object
You can also try this code with Online Java Compiler
Run Code
Since the Array class in the java.lang.reflect package has all the static methods. We can use all of its predefined functions simply by using the class name.
Syntax:
Array.<name of the function>;
You can also try this code with Online Java Compiler
Run Code
You can also read about the topic of Java Destructor and Hashcode Method in Java.
Creating a Dynamic Array Using Reflection Array Class
Following are the steps involved in creating an array using the Array class in the java.lang.reflect package.
- Firstly, determine the size of the array that has to be created.
- Secondly, using the newInstance() method of the Array class, we create the array. The newInstance() method receives the class of the array that we wish to create and the size of the array as the parameters.
Syntax:
A[] arrayName = (A[]) Array.newInstance(A.class, size);
You can also try this code with Online Java Compiler
Run Code
Example:
// import the Array class in the java.lang.reflect package
import java.lang.reflect.Array;
public class Main {
public static void main(String[] args) {
// we set the size of the array to be 5
int arraySize = 5;
// Create an integer array
// using reflect.Array class
// This is done using the newInstance() method
// we create an array "reflectArray" of type int.
// the newInstance() method receives the class of the type of the array that we wish to create
// In this case it is int. So we pass int.class as the value for the first parameter
// The second value is the size of the array that is to be created
int[] reflectArray = (int[]) Array.newInstance(
int.class, arraySize);
// right now the array is empty
// by default all the values are zero
for (int item : reflectArray) {
System.out.println(item);
}
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
0
0
0
0
0
You can also try this code with Online Java Compiler
Run Code
Explanation:
- In the above example, we create an array of int type. The first value for the newInstance() method was int.class. This tells the method that the user wishes to create an array of type int. We can similarly use float, double, etc.
- The second value for the newInstance() method was the size of the array that needed to be created.
- This function returns an object array which is then typecast. Here (int[]) before the Array.newInstance() is used to typecast the object array to int type.
- Creating an array using the Array class is very simple. One only needs to understand how to use the newInstance() method.
Also see, Swap Function in Java
Adding Elements in an Array Using Reflection Array Class
Following are the steps involved in adding elements in an array using the Array class in the java.lang.reflect package.
- Get the value that has to be added.
- Get the index at which the value has to be inserted.
- In order to add an element in an array (say of A class), use the setA() method of Array class, where A is replaced by the type of the array such as setInt(), setDouble(), etc.
- The setA() where A can be Int, Double, etc., takes three arguments. The first is the array in which the value has to be inserted. The second is the index at which the value is to be inserted, and the third and final argument that it takes is the value or the element that has to be inserted.
Syntax:
Array.setA(A[], index, valueToBeInserted);
You can also try this code with Online Java Compiler
Run Code
Example:
import java.lang.reflect.Array;
public class Main {
public static void main(String[] args) {
// Create an integer array of size 4.
// using reflect.Array class
// This is done using the newInstance() method
int[] myArray = (int[]) Array.newInstance(
int.class, 4);
// Add elements into the array
// This is done using the setInt() method
// Here Int is written with capital I.
Array.setInt(myArray, 0, 100);
Array.setInt(myArray, 1, 101);
Array.setInt(myArray, 2, 102);
Array.setInt(myArray, 3, 103);
// Print the items in the array
for (int item : myArray) {
System.out.print(item + " ");
}
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
100 101 102 103
You can also try this code with Online Java Compiler
Run Code
Explanation:
- To add an element to the array we take the help of the setInt() method in the above example.
- The Array.setInt() takes three arguments. Firstly the array in which the element has to be added. Secondly, the index at which element has to be added, and lastly, the value that has to be added.
- The setInt() method has I in capital letters.
- Suppose we try to add value for an illegal index java.lang.ArrayIndexOutOfBoundsException error will be thrown.
Retrieving Elements From an Array Using Reflection Array Class
Retrieving an element from an array using the Array class is very similar to the way we add elements. Instead of the setA() method, we use the getA() method, where A denotes the type of array.
Following are the steps involved in retrieving elements from an array using the Array class in the java.lang.reflect package.
- Get the index of the element that has to be retrieved from the array.
- We then use the Array.getA() method that receives two arguments. The first is the array A[], and the second is the index that has to be retrieved. Illegal indexes will result in an error.
Syntax:
Array.getA(A[], index);
You can also try this code with Online Java Compiler
Run Code
Example:
import java.lang.reflect.Array;
public class Main {
public static void main(String[] args) {
// Create an integer array of size 4.
// using reflect.Array class
// This is done using the newInstance() method
int[] myArray = (int[]) Array.newInstance(
int.class, 4);
// Add elements into the array
// This is done using the setInt() method
// Here Int is written with capital I.
Array.setInt(myArray, 0, 100);
Array.setInt(myArray, 1, 101);
Array.setInt(myArray, 2, 102);
Array.setInt(myArray, 3, 103);
// retrieve the elements using the Array.getInt() method
// the getInt() methods takes two arguments:
// 1-> the array. Here we pass myArray
// 2-> the index for which the element has to be retrieved
int firstElement = Array.getInt(myArray, 0);
System.out.println(firstElement);
int secondElement = Array.getInt(myArray, 1);
System.out.println(secondElement);
int lastElement = Array.getInt(myArray, 3);
System.out.println(lastElement);
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
100
101
103
You can also try this code with Online Java Compiler
Run Code
Practice it on online java compiler.
Explanation:
- To retrieve an element from the array we take the help of the getInt() method in the above example.
- The Array.getInt() takes two arguments. Firstly the array from which the element has to be retrieved, and secondly, the index for which we want to retrieve the element.
- If we try to retrieve an element from an illegal index, it will throw java.lang.ArrayIndexOutOfBoundsException error.
Methods in Reflection Array Class in Java
Object get(Object array, int index)
The Array.get() method returns the element of the array as a type of Object class.
Syntax:
Array.get(Object []array, int index)
You can also try this code with Online Java Compiler
Run Code
Instead of the generic Array.get(), we can have specific get() methods that are used to retrieve values of a particular data type. We don't have to typecast it.
Some of them are:
boolean getBoolean(Object array, int index): This method returns the element of the array as boolean type at the index provided in the argument.
byte getByte(Object array, int index): This method returns the element of the array as byte at the specified index.
char getChar(Object array, int index): This method returns the element of the array as char at the specified index.
double getDouble(Object array, int index): This method returns the element of the array as double at the specified index.
int getInt(Object array, int index): This method returns the element of the array as int at the specified index.
int getLength(Object array): This method returns the length of the specified array object as an int.
void set(Object array, int index, Object value)
The Array.set() is a void type method that doesn’t return any value. This method is used to set a specified value at a particular index in the object array.
Syntax:
Array.set(Object []array, int index, Object value)
You can also try this code with Online Java Compiler
Run Code
Instead of the generic Array.set(), we can have specific get methods that are used to set values of a particular data type.
Some of them are:
void setBoolean(Object array, int index, boolean val): This method is used to set a specified boolean value (true or false) to a specified index of a given object array.
void setDouble(Object array, int index, double d): This method is used to set a specified double value to a specified index of a given object array.
void setInt(Object array, int index, int i): This method is used to set a specified int value to a specified index of a given object array.
void setLong(Object array, int index, long l): This method is used to set a specified long value to a specified index of a given object array.
FAQs
1.What is the reflection array class in Java?
The Array class in the java.lang.reflect package is a Java reflection class. The Array class includes static methods that can be used to dynamically create and access Java arrays.
2. What is the parent class of java.lang.reflect.Array?
java.lang.Object
3. Which method is used to create arrays in Java reflection array class?
The Array.newInstance() method is used to create arrays in Java reflection array class.
4. What are the two arguments in the Array.get() method?
The first is the array A[], from which we want to retrieve the data, and the second is the index that has to be retrieved.
5. What are the three arguments in the Array.set() method?
The first is the array in which the value has to be inserted. The second is the index at which the value is to be inserted, and the third and final argument that it takes is the value or the element that has to be inserted.
Conclusion
In this article, we have extensively discussed the reflection array class in Java along with the code implementation of various methods in this class.
- The Array class in the java.lang.reflect package is a Java reflection class. The Array class includes static methods that can be used to dynamically create and access Java arrays.
- This class is final, which means that it cannot be altered or even instantiated. The methods contained within this class can be accessed by using the class name.
- The Array.newInstance() method is used to create arrays in Java reflection array class.
We hope that this blog has helped you enhance your knowledge regarding the reflection array class in Java and if you would like to learn more, check out our articles here.
Recommended problems -
Do upvote our blog to help other ninjas grow. Happy Coding!