About Monkey 2 › Forums › Monkey 2 Programming Help › Syntax for "Array of Arrays" (Jagged Array) ?
This topic contains 6 replies, has 5 voices, and was last updated by 
 codifies
 2 years, 2 months ago.
- 
		AuthorPosts
 - 
		
			
				
September 9, 2016 at 8:06 am #3816
What is the syntax for declaring this? (Not multidimentional arrays)
Since the below code compiles, I assume Arrays of Arrays are still in , but I can’t seem to get the declaration syntax right. Local jag:Float[][] = New ????
Local arrlist:= New List<Float[]>
arrlist.AddLast(New Float[4])
Local multi:= multi.ToArray()Function Test:Void(tes:Float[][])
End function
September 9, 2016 at 8:55 am #3819Quick demo:
Monkey12345678910111213141516171819Function Main()Local w:=20,h:=10Local t:=New Int[][w]For Local x:=0 Until wt[x]=New Int[h]NextFor Local y:=0 Until hFor Local x:=0 Until wPrint t[x][y]NextNextEndNote that the ‘new’ is sort of back to front, ie: in the ‘New Int[][w]’ bit, w comes last, but when you’re indexing with ‘x’ it comes first!
This actually makes sense to me as a compiler coder, ie: ‘New Int[][w]’ is w new int arrays, which is what we want, but I think java/c# parse things kind of differently so you end up with ‘New Int[w][]’ instead, which or may not make more sense to others.
I’m pretty relaxed about changing this if enough people prefer the Java way.
September 9, 2016 at 9:03 am #3820Hi Mark, thanks for clearing it up, I don’t think I would have figured that out
I’m all for changing it to the java/monkey1/c# for consistency between languages.
Also, it seems logical, that the major dimension, that holds the elements are declared first.
September 9, 2016 at 9:11 am #3822deleted.
January 22, 2017 at 6:37 pm #6738errr wot!
[/crayon]Monkey123456789101112[crayon-5cb9bda5403ec254894812 inline="true" ]Global a:Int[] = New Int[10]global b:Int[][]'Global c:Int[][] = New Int[10][10]'Global d:Int[][] = New Int[10]Int[10]'Global e:Int[][] = New Int[10] New Int[10]Function Main()End Functionif b works why the heck doesn’t c (like a few other languages) – do I really need a loop just to make an empty array?
January 22, 2017 at 7:50 pm #6740If you want some ‘rectangular array’ you can do the following:
[/crayon]Monkey1234[crayon-5cb9bda5431d8531085164 inline="true" ]Local map1:= new int[10,10]Local map2:= new int[7,9]or
[/crayon]Monkey1234567[crayon-5cb9bda5431de718646357 inline="true" ]Local map1:int[,]Local map2:Int[,]map1= new int[10,10]map2= new int[7,9]If you want an array of various arrays (non-rectangular/jagged) you’ll have to loop as mark showed. h could have different values during the loop.
January 22, 2017 at 11:39 pm #6741…ah jee that should be in the docs!
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.