- Home /
How to Create Dynamic Multidimensional Array?
I need to create a 3D array that can be resized per-dimension. My first thought was to create a 1D array and "simulate" the 3D array (like thing[(height*y)+x]
or whatever it is) but I don't believe that would allow for resizing. I googled it but all I could find was how to make static multidimensional arrays.
How would I achieve this in JavaScript/UnityScript?
Answer by Benjames · May 30, 2015 at 06:24 AM
I know with c# you can create Lists that allow for resizing.
http://stackoverflow.com/questions/10115769/something-similar-to-c-sharp-net-generic-list-in-java
Ya java has lists google "c# list java equivalent" :))
So I believe you can make a multidimensional array like this:
var mda=new float[300,50,300];
to re size you would probably have to make a new array and just transfer the values
I suggested Lists because they can be re sized, although it might be tough to make a multidimensional list.
http://stackoverflow.com/questions/4401850/how-to-create-a-multidimensional-arraylist-in-java
You could make a class with three separate Lists.
class md
{
List<float> x;
List<float> y;
List<float> z;
}
so when you're adrressing it you would be like md.x or md.z that type thing
Sorry I couldn't help more.
@Benjames The fact that it's tough to make a multidimensional list is why I posted the question. Also, your second code-box wouldn't work. Each subsequent array needs to be contained within the last.
I did attempt to create a multidimensional List like this:
var room:List.<List.<List.<roomInfo> > >;
room=new List.<List.<List.<roomInfo> > >();
room.Add(new List.<List.<roomInfo> >());
room[0].Add(new List.<roomInfo>());
room[0][0].Add(new roomInfo());
As you can see that's pretty confusing already, but then try resizing the array without making it jagged... there has to be a better way...