Syntax for "Array of Arrays" (Jagged Array) ?

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, 4 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #3816

    Diffrenzy
    Keymaster

    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

    #3819

    Mark Sibly
    Keymaster

    Quick demo:

     

    Note 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.

    #3820

    Diffrenzy
    Keymaster

    Hi 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.

    #3822

    nerobot
    Participant

    deleted.

    #6738

    codifies
    Participant

    errr wot!

    [/crayon]

    if 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?

    #6740

    abakobo
    Participant

    If you want some ‘rectangular array’ you can do the following:

    [/crayon]

    or

    [/crayon]

    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.

    #6741

    codifies
    Participant

    …ah jee that should be in the docs!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.