I need to create an array at running time according to the values of the input array tableParameter.
An example of my code is as follows:
int[] tableParameter = new int[dimension + 1];
tableParameter[0] = N;
for(int i = 1; i < tableParameter.length; i++)
tableParameter[i] = i;
Object myArray = Array.newInstance(int.class, tableParameter);
//set the index 1 of the array with the value 100
Array.setInt(myArray, 1, 100);
The exception occurs in the last line of the code above:
Exception in thread "main" java.lang.IllegalArgumentException: Argument is not an array
But when I used the
System.out.println(myArray.getClass().getCanonicalName());
to verify the class of myArray, it prints out int[][][][], which means myArray is definitely an array type.
So why the JVM throws the exception that myArray is not an array?