How do you declare 2d lists

john2367

New Member
Joined
Feb 19, 2019
How do you actually declare 2d lists in cerberus x?
Cerberus X:
Field 2dListForAnObject:List<List <Object>>
Is the code above the right syntax?
 

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Location
Germany
2D list? Do you mean a 2D array?

If the later, then this should show it well.
Cerberus X:
Strict

Import mojo

'-----------------------------------------------------------------
Class myClass Extends App
    ' Declare an 2D array
    Field mA:Int[][]
    '-----------------------------------------------------------------
    Method OnCreate:Int()
        SetUpdateRate 60
        ' Initialze the first dimension
        mA = New Int[10][]
        For Local x:Int = 0 To mA.Length()-1
            ' Initialize the second dimension
            mA[x]= New Int[15]
            For Local y:= 0 To mA[x].Length()-1
                ' Fill values into your array
                mA[x][y] = x*y
            Next
        Next
        Return 0
    End
    '-----------------------------------------------------------------
    Method OnRender:Int()
        Cls 155,0,0
        For Local x:Int = 0 To mA.Length()-1
            For Local y:= 0 To mA[x].Length()-1
                DrawText mA[x][y], x*25+150, y*25+50, .5, .5
            Next
        Next
        Return 0
    End
End

'-----------------------------------------------------------------
Function Main:Int()
    New myClass
    Return 0
End
 
Last edited:

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
Variable names must start with a letter or an underscore character. So instead of "2dListForAnObject" write something like "my2dList". Remember that this just declares just a variable – the value is still Null. In order to create a list object you can write something like this:
Cerberus X:
Field  my2dList := New List<List<Object>>
To add a list to your list write:
Cerberus X:
my2dList.AddLast(New List<Object>)
Btw, for what do you need this 2d list structure? Depending on what you want to do some other data structure might be a better choice.
 
Top Bottom