-
April 5th, 2005, 01:52 PM
#1
Help with Java needed.
I want to create a program which dynamically creates multidimensional arrays, i.e. the program does not know how many dimensions the array will have as this is specified upon execution.
Code:
import java.lang.reflect.*;
class Stuff {
public static void main(String[] args) {
int[] dimensions = new int[Integer.parseInt(args[0])];
for (int i = 0; i < dimensions.length; i++) {
dimensions[i] = 10;
}
int[][][][] stuff = (int[][][][])Array.newInstance(int.class, dimensions);
stuff[9][8][9][9] = 8;
}
}
It is possible to create such arrays by using Array.newInstance(), this method returns an Object and it must be typecasted if being used as in the example.
For example:
Code:
int[] dimensions = {10, 10, 10, 10};
int[][][][] stuff = (int[][][][])Array.newInstance(int.class, dimensions);
This will create a 4 dimensional array with 10 elements in each 'dimension'.
Now the problem is that having to use the constructor int[][][][] defeats the point of that method, as one still has to know how many dimensions the array will have.
Does anybody know how to avoid this?
As stated, I want a program that creates an n-dimensional array, where n is specified by the user.
The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me
www.elhalf.com
-
April 5th, 2005, 03:14 PM
#2
"When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
"There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
"Mischief my ass, you are an unethical moron." - chsh
Blog of X
-
April 5th, 2005, 06:59 PM
#3
Hm, I've never had the need to use any of those.
They seem to be the solution, thanks.
The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me
www.elhalf.com
-
April 6th, 2005, 12:49 AM
#4
"When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
"There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
"Mischief my ass, you are an unethical moron." - chsh
Blog of X
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|