Reputation: 121
I have just started programming in Java again and I am having some trouble. What I want to do is create an object that will be an array of arbitrary dimension. That is it will be an array of arrays of arrays... and so on. What I was thinking of doing was making a class that is an array of arbitrary objects. Thus I could just create the multidimensional array of dimension d by making an array of multidimensional arrays with dimension d-1. The code, I think, would look something like this:
Array[] multiArray;
public MArray(int d){
if(d<0){MArray(d) = null;}
else{multiArray = MArray(d-1);}
}
However I don't know if Array[] is the correct thing to do to create an array of arbitrary objects. I seem to recall when I was learning Java a few years ago there was a way you could it but I can't remember and my Google searches are proving fruitless.
Upvotes: 2
Views: 1035
Reputation: 1674
Multidimensional arrays in Java can be created with any Object type(including Object itself). Therefore, you can do a simple declaration like
Object[][] my2DimArray = New Object[d-1][d-1]
Upvotes: 1