Creating an Array
Home ] Up ] Reconstructing a Class ] Inspecting a Class Instance ] Instantiating an Object ] [ Creating an Array ] Calling a Method ] An Example ]

 

 

The Array class also has two newInstance() methods that allow you to dynamically create an array of a specified size and type:

  • To create a single dimension array
public static Object newInstance(Class componentType,
                                 int length) 
      throws NegativeArraySizeException

which does the equivalent of:

new componentType[length]
  • To create a multiple dimension array
public static Object newInstance(Class componentType,
                                 int[] dimensions) 
    throws IllegalArgumentException, 
           NegativeArraySizeException

which does the equivalent of

new componentType[dimensions[0]][dimensions[1]]...
 
The page was last updated February 19, 2008