File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes creating array  at run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating array  at run time" Watch "creating array  at run time" New topic

creating array at run time

meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
i dunno if this can be done...but do help me out...
i have a function that creates an array.... it takes the type of the aray as an argument...
as in:
if i want to create an array of doubles.... is it possible??? and how do i go about is a bit urgent...
thanx in advance...
marilyn murphy
Ranch Hand

Joined: Aug 28, 2001
Posts: 84
The way to create an array of doubles is:

double[] nameOfArray = new double[sizeOfArray];

Why do you need a function?
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
c...the thing is that if i pass "double" to the function, i want to create an array of doubles, if i pass "int" to the function, i want to an array of ints to be created.... got it???
in the function it should take the argument and create the array of that type in the same way as it is done(as you pointed out to me.... )
thanx anyways...
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

Well since you can't write a single function that can be passed either a double or an int, it's kind of a nonsense question, no?
What you can do is pass a Class to a function to tell it what class to deal with.
For the primitive types, Java declares Class instances such as Integer.TYPE, Double.TYPE, and so on (not to be confused with Integer.class and ilk).
So what are you really trying to accomplish? It sounds rather needlessly convoluted.

[Asking smart questions] [About Bear] [Books by Bear]
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
sorry if i didnt put my question across properly...
what i want to do is have a function :
createArray(String type)
i pass the type of array i want as this parameter(type) of type String, i mean when i call this function i will give the function call:
createArray(new String("int"));
createArray(new String("double"));
and my function should create an array of that type....
i hope i was clearer now. I just want to know if this is possible and how do i go about it...
Greg Charles

Joined: Oct 01, 2001
Posts: 2968

You can, of course, write code to create different types of arrays based on a String parameter. The problem comes with the return value. There is no way of generically referring to something that could be an array of integers or could be an array of doubles. You can get close by using an array of Objects and wrapper classes.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Actually, you can create arrays of primitives by specifying the return type as just Object. Arrays of primitives are considered descendants of Object so this will work.
As far as creating the array, you don't need an if statement for each type. There are methods in the Class class that allow you to create an instance of an object based on a String. Also, there is an Array class that already has a function to create an array with a dynamic type. Check out the Java API docs. Array is in the java.lang.reflect package.
If you aren't allowed to use this function, then you can easily write your own using the Class class and its functions.
To find out more details, use Google to search for information about reflection.

Java API Documentation
The Java Tutorial
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
Thanx a lot guys....
one more problem....if i return an array of the "object" class, can i typecast it to the required primitive datatype??
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You shouldn't return an array of Object (Object[]) - just return a plain Object (no []). Then you can cast it to whatever it really is. An int[] is not an Object[], but it is an Object.
In fact Java already has a method to do what you're asking for, in java.lang.reflect.Array:

"I'm not back." - Bill Harding, Twister
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
Thanx a ton.... got it now....
I agree. Here's the link:
subject: creating array at run time
It's not a secret anymore!