I want a function / data structure that can do this:
func(int dim){
if(dim == 1)
int[] array;
else if (dim == 2)
int[][] array;
else if (dim == 3)
int[][][] array;
..
..
.
}
anyone know how?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Edit
Or you could use Array.newInstance(int.class, sizes). Where sizes is an
int[]containing the desired sizes. It will work better because you could actually cast the result to anint[][][]...Original Answer
You could use the fact that both
int[]andObject[]areObjects. Given that you want a rectangular multidimensional array with sizes given by the list sizesYou lose all static type checking, but that will happen whenever you want a dynamically dimensioned array.