Create new Array instance
static Object newInstance(Class<?> componentType, int... dimensions)
- Creates a new array with the component type and dimensions.
static Object newInstance(Class<?> componentType, int length)
- Creates a new array with the component type and length.
import java.lang.reflect.Array;
public class Main {
public static void main(String[] argv) throws Exception {
int[] ints = (int[]) Array.newInstance(int.class, 10);
String[] stringArray = (String[]) Array.newInstance(String.class, 10);
}
}
Create multidimensional array
import java.lang.reflect.Array;
public class Main {
public static void main(String[] argv) throws Exception {
int[][] ints = (int[][]) Array.newInstance(int.class, 10,5);
}
}
Home
Java Book
Reflection
Java Book
Reflection
Array:
- Array
- Get the length of an Array
- Get value from an Array
- Create new Array instance
- Set value to an Array