dynamic arrays

S

segex

Hi all

I was wondering if it is possible to make an array dynamicly. I don't
the easy newbie stuff like
-----
int[] meth(int size) {
return new int[size];
}
-----
I actually more though of making an array of whatever (primitiv-type or
class and all sorts of dimensions)
so more somthing like:
-----
/*
* Signature is something similar to
* [I for int[] or
* [[Ljava.lang.String for String[][]
* etc
*/
Object makeArr(String signature) {
return <made array>
}
 
T

Thomas Hawtin

segex said:
/*
* Signature is something similar to
* [I for int[] or
* [[Ljava.lang.String for String[][]
* etc
*/
Object makeArr(String signature) {
return <made array>
}

java.lang.reflect.Array.newInstance(Class<?>,int)

Typical implementations of Collection.toArray(Object[]) use it.

Tom Hawtin
 
T

Thomas G. Marshall

freesoft_2000 coughed up:
Hi everyone,

You can use the Java ArrayList class

Richard West


Have you lost your @#$%ing mind, or did you simply not read his post?
 
R

Raymond DeCampo

Thomas said:
freesoft_2000 coughed up:




Have you lost your @#$%ing mind, or did you simply not read his post?

I thought he was just giving people permission to use the ArrayList
class. After all, he didn't quote anybody, so who knows? And he did
address the post to everybody, not just the OP.

So now we can rest easy knowing that Richard West is allowing us to use
the ArrayList class. The one I'm sweating out is HashMap....

Ray
 
R

Roedy Green

java.lang.reflect.Array.newInstance(Class<?>,int)

the other way to do it without reflection would be a switch on
dimension to create 1 to 10 dimensions

e.g.

case 3:
return new Qbject[a0][a1][a2];
 
T

Thomas Hawtin

Roedy said:
java.lang.reflect.Array.newInstance(Class<?>,int)
^length


the other way to do it without reflection would be a switch on
dimension to create 1 to 10 dimensions

e.g.

case 3:
return new Qbject[a0][a1][a2];

A different issue. I believe G. Segex was primarily looking at creating
arrays of different types rather than dimensions. You can't switch on
all possible types ever.

To create an array (of arrays)^n use:

java.lang.reflect.Array.newInstance(Class<?>,int[] dimensions)

Tom Hawtin
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top