Re: Create an Object from an Array Class



Joshua Cranmer wrote:
What you are probable intending is something like this:

Method m = Class.forName("some.pkg.Type").getMethod("main",
String[].class);
m.invoke(null, new String[0]);

If the type of array must be determined at runtime, the following idiom can also be used:

// arrayType is a class representing the type we want.
Object array = Array.newInstance(arrayType, 0); // Empty array

Array has another overloaded version that handles multiple array dimensions.

--
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
.



Relevant Pages

  • Re: Array defaults vs exception overhead
    ... The following idiom happens often in my code: ... the above does the hash lookup twice. ... It sort of depends on what percentage of time you expect the array slot ...
    (comp.lang.tcl)
  • Re: bullet proof for loop over $(find) in bash, how?
    ... : In an idiom like ... how can I make sure that I execute "something" exactly for ... be possible to fill an array with each file name found, ... This solution will not be perceptibly slower than a straight pipe. ...
    (comp.unix.shell)
  • Re: char data[0]
    ... an array of size 1 is used in C89/90. ... that this idiom is so widely used and recognized that the latest C ... member contributes 0 to the total size of the structure. ...
    (comp.lang.c)
  • Re: sizeof in old C compilers
    ... The code is filled with this idiom on memsets (and memcpys and ...). ... function where array is a pointer *and* changing the type of array. ...
    (comp.lang.c)
  • Re: sizeof in old C compilers
    ... The code is filled with this idiom on memsets. ... pass the array in is as a pointer and sizeof the pointer will not be ...
    (comp.lang.c)