How to create an array from a Class?

J

Joona I Palaste

Suppose I have a Java Class object denoting some Java class. I would
like to dynamically create an *array* of references to the class that
the Class object denotes. The size of the array should also be given in
the creation. What is the easiest way of doing this?

Example of what I have in mind:

Class c = "Hello, world!".getClass();
String[] array = /* what the heck do I write here? */;
array[0] = "Hello, world!";
for (int i=0; i<array.length; i++) {
System.out.println(array);
}

Should print "Hello, world!");

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The truth is out there, man! Way out there!"
- Professor Ashfield
 
D

David Zimmerman

Joona said:
Suppose I have a Java Class object denoting some Java class. I would
like to dynamically create an *array* of references to the class that
the Class object denotes. The size of the array should also be given in
the creation. What is the easiest way of doing this?

Example of what I have in mind:

Class c = "Hello, world!".getClass();
String[] array = /* what the heck do I write here? */;
array[0] = "Hello, world!";
for (int i=0; i<array.length; i++) {
System.out.println(array);
}

Should print "Hello, world!");


Array.newInstance(Class, int)
 
J

Joona I Palaste

Array.newInstance(Class, int)

Thanks!

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top