This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating array  at run time" Watch "creating array  at run time" New topic
Author

creating array at run time

meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
hi!
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:
arr1(double)
if i want to create an array of doubles.... is it possible??? and how do i go about it....it is a bit urgent...
thanx in advance...
meera
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
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

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.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [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"));
or
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...
Thanx...
Meera
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2833
    
  11

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.
HTH
Layne


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
Wanderer
Sheriff

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....
 
 
subject: creating array at run time
 
Similar Threads
How do you add to Arrays together?
compare array to an array object?
Strign of arrays
array dimension confusion
array objects