Multiple dim arrays (more then two) and length

Monkey Forums/Monkey Beginners/Multiple dim arrays (more then two) and length

Pakz(Posted 2015) [#1]
I thought I seen how it was done but I could not find it anymore. So how would I create a array that has more then two dimensions? I needed that feature a while ago and worked around it by storing the data in a string and reading it out like that. Quite a workaround though.

This is how I set up two dimensional arrays

Global map:Int[mapwidth][]
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next


Also, how do you use Length with multiple dimensional arrays? Is that even possible? The way I remembered and used it in Java did not seem to work with Monkey.

like:
mapwidth = map[].Length
mapheight = map.Length


Can anyone who knows this show me how to do it? I could use this to make map editing in text files easier.


dawlane(Posted 2015) [#2]
To get the width you have to pass a row element.
mapwidth = map[0].Length
mapheight = map.Length


A quick search for arrays should show a few useful bits.
http://www.monkey-x.com/Community/_search.php?bbs=&forum=228&terms=arrays&method=simple&case=no&user=

Strict

' You can have as many rows as you like, but the number of elements in a row must remain constant
Global staticArray:Int[][] = [
				[2,4,6,8,10],
				[1,3,5,7,9],
				[0,0,0,0,0]]

Function Main:Int()
	Print staticArray[1][4]		' Prints 9
	Print staticArray[0].Length()	' Print the number of elements in a row 5
	Return 0
End



Pakz(Posted 2015) [#3]
Ah, I tried that but I must have done something wrong then. It works now. Thanks

Now I only need to know how to set up arrays with more then 2 dimensions.


dawlane(Posted 2015) [#4]
This should be a place to start.
http://www.monkey-x.com/Community/posts.php?topic=7817&post=79041&view=all#79041
Read Gerry Quinn and Danilo posts. They show 3D arrays.


Pakz(Posted 2015) [#5]
I experimented a bit after looking at the posts and I think I got 3 dimensional arrays working.

Here is what works for me until now :

Import mojo

Global mapwidth:Int=3
Global mapheight:Int=3
Global mapdepth:Int=3

Global map:Int[mapwidth][][]

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight][]
            For Local z=0 Until mapdepth
            	map[i][z] = New Int[mapdepth]
            Next
        Next
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
        	map[x][y][0] = Int(Rnd(3))
        	For Local z=1 Until mapdepth
        		map[x][y][z] = Rnd(3,10)
        	Next
        Next
        Next
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "3 Dimensional arrays and Length example",0,0
        For Local y=0 Until map[0].Length
        For Local x=0 Until map.Length
		For Local z=0 Until map[0][0].Length
			Local s:String="x:"+x+" y:"+y+" z:"+z+" = "
			DrawText s+""+map[x][y][z],x*132,y*64+(z*15)+32
		Next
        Next
        Next
    End Method
End Class

Function Main()
    New MyGame()
End Function



Samah(Posted 2015) [#6]
@dawlane: ' You can have as many rows as you like, but the number of elements in a row must remain constant

False. The number of elements do not have to match at all. In this case it's often called a "jagged" array.
They only have to match if you want to visualise your "multidimensional array" as a rectangle.


dawlane(Posted 2015) [#7]
@Samah: You are correct. When I tested the little snippet of my code above with as a staggered array it didn't compile. I must have not removed the surplus comma. Doh!


Samah(Posted 2015) [#8]
@dawlane: You are correct.

Technically, yes, but jagged arrays are ugly as hell. XD


dawlane(Posted 2015) [#9]
@Samah: yes, but jagged arrays are ugly as hell.
Agreed. It can also increase the chance of accessing the array out of bounds if no checking is implemented, which of course will add addition overhead if such checking is implemented. I think it's always best to follow the K.I.S.S. principle.


ziggy(Posted 2015) [#10]
After this discussion: http://www.monkey-x.com/Community/posts.php?topic=6170&post=70934&view=all#70934

I use this multi-dim array: https://code.google.com/p/lemongames/source/browse/collections/multiarray.monkey

I found it to work quite fast, and I find it nicer than working with arrays of arrays.