Copy array value with Array Reflection in Java
Description
The following code shows how to copy array value with Array Reflection.
Example
/*w w w .ja v a 2s . c om*/
import java.lang.reflect.Array;
public class Main {
public static void main(String[] args) {
int[] sourceInts = { 12, 78 };
int[] destInts = new int[2];
copyArray(sourceInts, destInts);
String[] sourceStrgs = { "Hello ", "there ", "everybody" };
String[] destStrgs = new String[3];
copyArray(sourceStrgs, destStrgs);
}
public static void copyArray(Object source, Object dest) {
for (int i = 0; i < Array.getLength(source); i++) {
Array.set(dest, i, Array.get(source, i));
System.out.println(Array.get(dest, i));
}
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »