Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 938

Reflection in Java - Array

$
0
0

Array in Java is also a class so many of the methods in java.lang.Class may be used with the array.

Reflection in Java also provides a specific class java.lang.reflect.Array for arrays.

How to identify array

If you want to determine if a class member is a field of array type that can be done by invoking Class.isArray() method.

Example code

For the example let’s say we have a class TestClass which has one array field numArray. In another class ReflectArray using the class.isArray() method it is determined if there is any array in the TestClass.

TestClass


public class TestClass {
private int value;
private int[] numArray;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int[] getNumArray() {
return numArray;
}
public void setNumArray(int[] numArray) {
this.numArray = numArray;
}
}

ReflectArray


import java.lang.reflect.Field;

public class ReflectArray {

public static void main(String[] args) {
try {
Class<?> c = Class.forName("org.netjs.prog.TestClass");
Field[] fields = c.getDeclaredFields();
for (Field f : fields) {
Class<?> type = f.getType();
// Looking for array
if(type.isArray()){
System.out.println("Array found " + f.getName());
}
}

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Output


Array found numArray

Creating new array refelctively

Using reflection you can dynamically create arrays of arbitrary type and dimensions using java.lang.reflect.Array.newInstance() method.

Example code


int[] testArray = (int[])Array.newInstance(int.class, 5);
System.out.println("length of array " + testArray.length);

Output


length of array 5

Getting and setting array

Using reflection you can get or set an entire array. You can set an entire array using Field.set(Object obj, Object value) method. To retrieve an entire array use

Field.get(Object obj)

method.

If you want to get or set an array individual element by element, Array class has methods for that also. In order to set or get an int array you can use setInt(Object array, int index, int i) and getInt(Object array, int index) methods. Same way if you have a float array you can use setFloat(Object array, int index, float f) and getFloat(Object array, int index) methods.

There is also a method which takes Object for value as parameter that can be used with any type - set(Object array, int index, Object value) and get(Object array, int index) methods.

Example code


int[] testArray = (int[])Array.newInstance(int.class, 5);
System.out.println("length of array " + testArray.length);
// Setting values using setInt and set methods
Array.setInt(testArray, 0, 1);
Array.set(testArray, 1, 2);
Array.setInt(testArray, 2, 3);

// Getting values using getInt and get methods
System.out.println("First element " + Array.get(testArray, 0));
System.out.println("Second element " + Array.getInt(testArray, 1));
System.out.println("Third element " + Array.getInt(testArray, 2));

Output


length of array 5
First element 1
Second element 2
Third element 3

That's all for this topic Reflection in Java - Array. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Reflection in Java - Field
  2. Reflection in Java - Method
  3. Reflection in Java - Constructor

You may also like -

>>>Go to Java advance topics page


Viewing all articles
Browse latest Browse all 938

Trending Articles