java.lang.reflect.Array Example
In this example we shall explain how to use java.lang.reflect.Array. The java.lang.reflect.Array class provides static methods to dynamically create and access Java arrays. Arrays are objects that contain values of the same type, with immutable length. So, an array’s components of the same type and their number is always fixed. The length of an array must be set when an array is created, whereas the type of the components may be primitive (int
, byte
, long
), an object type (String, Integer, Long), or even another array, but is always single.
Below, we make use of the methods provided in the java.lang.reflect.Array class, in order to create new instances of arrays, set values to their components, get their components values, get the types of the components of every array and finally get each array’s length.
- We create a new instance of an array, of any of the types mentioned above, using the
newInstance(Class> componentType, int... dimensions)
(when we want to set many dimensions), ornewInstance(Class> componentType, int length)
(when we only want to set the array number of components) methods of java.lang.reflect.Array. - In order to set the value of the indexed component of an array, we use the
set(Object array, int index, Object value)
orset...(Object array, int index, ... value)
API methods of java.lang.reflect.Array. - In order to get the value of the indexed component of an array, we use the
get(Object array, int index)
orget...(Object array, int index)
API methods of java.lang.reflect.Array. - In order to get the type of the array’s components, we first get the Class of the array, making use of
getClass()
API method of Object, and then we use thegetComponentType()
method of Class. Note that all components in an array are of the same type. - In order to get an array’s length we use the
getLength(Object array)
API method of java.lang.reflect.Array. Note that the array’s length is fixed and must be set in initialization of an array.
Create an Array
Access an array’s components
Obtain the type of an array’s components
Get the number of an array’s components
Always note that an ArrayIndexOutOfBoundsException may occur, when trying to access a specified index which is negative, greater than or equal to the length of the specified array.
ArrayExample.java
package com.javacodegeeks.snippets.core; import java.lang.reflect.Array; public class ArrayExample { private static String[] strArr = (String[]) Array.newInstance(String.class, 10); private static int[] intArr = (int[]) Array.newInstance(int.class, 10); public static void fillArrays() { for(int i=0; i<=9; i++){ Array.set(strArr, i, String.valueOf(i)); Array.setInt(intArr, i, i); } } public static void showArrays() { System.out.println("-Arrays have: "); for(int i=0; i<=9; i++){ System.out.println("At position " + i + " strArr component is :" + Array.get(strArr, i)); System.out.println("At position " + i + " intArr component is :" + Array.getInt(intArr, i)); } } public static void main(String[] args) { fillArrays(); showArrays(); System.out.println("Components of the strArr are of type : " + strArr.getClass().getComponentType()); System.out.println("Length of the strArr is : " + Array.getLength(strArr)); System.out.println("Components of the intArr are of type : " + intArr.getClass().getComponentType()); System.out.println("Length of the intArr is : " + Array.getLength(strArr)); } }
If you run the example above, you will get the result below:
Output
-Arrays have: At position 0 strArr component is :0 At position 0 intArr component is :0 At position 1 strArr component is :1 At position 1 intArr component is :1 At position 2 strArr component is :2 At position 2 intArr component is :2 At position 3 strArr component is :3 At position 3 intArr component is :3 At position 4 strArr component is :4 At position 4 intArr component is :4 At position 5 strArr component is :5 At position 5 intArr component is :5 At position 6 strArr component is :6 At position 6 intArr component is :6 At position 7 strArr component is :7 At position 7 intArr component is :7 At position 8 strArr component is :8 At position 8 intArr component is :8 At position 9 strArr component is :9 At position 9 intArr component is :9 Components of the strArr are of type : class java.lang.String Length of the strArr is : 10 Components of the intArr are of type : class int Length of the intArr is : 10
Download the Eclipse Project
This was an example of java.lang.reflect.Array.
You can download the full source code of this example here: java.lang.reflect.ArrayExample