Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating array at run time

 
meera sood
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way to create an array of doubles is:

double[] nameOfArray = new double[sizeOfArray];

Why do you need a function?
 
meera sood
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65338
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
meera sood
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3002
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
meera sood
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
meera sood
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a ton.... got it now....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic