About Monkey 2 › Forums › Monkey 2 Programming Help › Way to predefine/inline define an array of arrays?
This topic contains 8 replies, has 6 voices, and was last updated by 
 Mark Sibly
 2 years, 1 month ago.
- 
		AuthorPosts
 - 
		
			
				
February 25, 2017 at 2:14 pm #7297
is there a way to define an array of arrays? something like in monkey1.
I tried this but it doesn’t work:Global data:String[][] = New String[][]([“cat”,”dog”],[“monkey”,”parrot”])
February 25, 2017 at 2:44 pm #7299you can have multidimensional arrays Myarr[x,x] or arrays of arrays Myarr[][] depending on what you need.
multidimensional arrays are much simpler.
you can directly initialize simple arrays only.simple and multidimentionnal arrays are explained in the latest docs (in github) but the arrays of arrays are not explained (see link)
http://monkey2.monkey-x.com/forums/topic/syntax-for-array-of-arrays-jagged-array/an example of multidimentionnal array:
[/crayon]Monkey123456789101112[crayon-5cba1553bec52563384396 inline="true" ]#Import "<std>"Using std..Function Main()Local strs:=New String[2,2]strs[0,0]="hip"strs[0,1]="hop"strs[1,0]="get"strs[1,1]="hype"Print strs[1,0]EndFebruary 25, 2017 at 5:53 pm #7300I know how to fill it one item at a time. I just don’t know how how to or if there is a way to do it in a single instruction.
February 26, 2017 at 12:58 pm #7304you can only predefine simple arrays so it will take two lines with your array of array example([][]). With a multidimensional([,]) array it’s not possible at all for now.
this is the most you can have for now i think..
[/crayon]Monkey123456789101112131415[crayon-5cba1553c3f25441934868 inline="true" ]#Import "<std>"Using std..Function Main()Local t:=New String[][2]t[0]=New string[]("cat","dog")t[1]=New string[]("ape","monkey")Print t[1][0]EndFebruary 26, 2017 at 10:05 pm #7307That’s exactly what I didn’t want to do. I guess I’ll have to settle for that.
Thanks.February 27, 2017 at 2:08 am #7310New String[][]([“cat”,”dog”],[“monkey”,”parrot”])
AFAIK there is no such syntax, and I would like to have it too.
February 28, 2017 at 8:47 am #7322I second that.
BTW: I’ve seen that there are two kinds of multidimensional arrays?
Monkey12Local arr1[,] = New Int[10,10]Local arr2[][] = New Int[][10]Why is that?
February 28, 2017 at 5:30 pm #7328@xaron
New Int[10,10] is a multi-dimensional 10×10 array.New Int[][10] is an array of arrays, and each array can be different in size:
Monkey12345678910111213141516171819202122#Import "<std>"Using std..Function Main()Local arr:=New Int[][3]arr[0] = New Int[2] ' create new Int-Array with 2 slots (0 and 1)arr[1] = New Int[4] ' create new Int-Array with 4 slots (0 to 3)arr[2] = New Int[6] ' create new Int-Array with 6 slots (0 to 5)arr[0][1] = 2arr[1][3] = 4arr[2][5] = 6Print arr[0][1]Print arr[1][3]Print arr[2][5]EndFebruary 28, 2017 at 10:24 pm #7333This works…
Monkey1234567891011121314Function Main()Local t:=New String[][](New String[]( "Cat","Dog" ),New String[]( "Kitten","Puppy" ) )For Local x:=Eachin tFor Local y:=Eachin xPrint yNextNextEnd - 
		AuthorPosts
 
You must be logged in to reply to this topic.