3 Dimensional Array?

Monkey Forums/Monkey Programming/3 Dimensional Array?

Chroma(Posted 2013) [#1]
I'm looking for the best way to implement layers to my map. I'd like to have up to 4 layers. I will be cycling through and doing a DrawImage to display it. What is the best method? List, Array, Map, Stack?

I can do a 2 dimensional array. How would I do a 3 dim one? Is it even needed?


Gerry Quinn(Posted 2013) [#2]
You can actually use any collection you prefer for each dimension. In practice, of course, it is advisable to avoid anything too complicated.

A 3D array is simply an array of 2D arrays[*]. Probably if you have a world with four 'heights' this is the correct approach.

[*] Just as a 2D array is simply an array of 1D arrays.


Jesse(Posted 2013) [#3]
Class MakeArray<T>

	Function ThreeD:T[][][](x:Int,y:Int,z:Int)
		
		Local arr:T[][][] = New T[x][][]
		
		For Local i:Int = 0 Until x
			arr[i] = New T[y][]
		Next
		
		For Local i:Int = 0 Until x
			For Local j:Int = 0 Until y
				arr[i][j] = New T[z]
			Next
		Next
		
		Return arr		
	End Function

	Function TwoD:T[][](x:Int,y:Int)
		Local arr:T[][] = New T[x][]
		For Local i:Int = 0 Until x
			arr[i] = New T[y]
		Next
		Return arr		
	End Function

End Class