Lists in Monkey

Monkey Archive Forums/Monkey Tutorials/Lists in Monkey

EdzUp(Posted 2012) [#1]
Unlike BlitzMax lists monkey lists have to be initialised like this:
Class myClass
'insert your field variables here
End Class

Class declarations are roughly the same as Types in BlitzMax they operate relatively similar and do the same function.

Now we need to create a list so we can access a list of them

Local myList:List<myClass> ' this means you can only insert objects of Class 'myClass'.
myList = New List<MyClass>

Notice I added a MyList = New List<MyClass> the reason for that line is to initialise the list for use if you dont have that you will get a error stating that your list is Null.

After that you can do things like:
Local var = new MyClass
myList.AddLast( var )


However you cannot add other things to the list, lists in monkey are fixed to a specific class in our instance its MyClass so if you had another class called MyOtherClass and tried to add it to myList it would throw an error as the list hasnt been created for that class.

The code was borrowed from the forum I dont know who done it as a search didnt help either.


Jesse(Posted 2012) [#2]
That is good Ed but don't forget that you can have different classes in the list as long as they have the same base class and the list is of base class.


EdzUp(Posted 2012) [#3]
yeah I just put it up here for newbies coming to monkey from blitzmax as its a whole different beast.


DruggedBunny(Posted 2012) [#4]
Good explanation, but there's a little typo in the second codebox and the following sentence -- MyList should be myList.


Shinkiro1(Posted 2012) [#5]
I appreciate your effort, but I found your tutorial a little bit confusing.
Are you targeting general newbies or people that came over from BlitzMax?

The only thing bmax people need to know is the initialization
[monkeycode]myList:List<myClass>[/monkeycode]
.. and that a list can only hold a value from one specific class (which is defined inside <>)
Other than that, nothing has really changed from how you use them.

On the other hand, for absolute newbies an example would be better suited I think (nothing abstract).


Samah(Posted 2012) [#6]
If this is a tutorial for people new to Monkey, you should probably use the general "agreed upon" Monkey coding standards.
For example, classes (myClass) and keywords (new) should start with uppercase.


therevills(Posted 2012) [#7]
Thats a good start EdzUp, but I think runnable code is always helpful too:

[monkeycode]' Monkey List example with user objects
' http://blitz-wiki.appspot.com/List

' Use strict to catch more user errors
Strict

' Main entry point for a Monkey program
Function Main:Int()

' create a list that can hold MyClass objects
Local myList:List<MyClass> = New List<MyClass>

' creata a bunch of objects and add them to the list
For Local i:Int = 0 To 10
myList.AddLast(New MyClass)
Next

' output how many items in the list
Print "No. of items in list = " + myList.Count()

' Output the list
For Local i:MyClass = Eachin myList
Print i.x + "," + i.y
Next

' add ExtendClass to myList
myList.AddLast(New ExtendedClass)

Print "No. of items in list = " + myList.Count()

Return 0
End

' Create the custom class
Class MyClass
Field x:Float
Field y:Float

' Simple constructor
Method New()
x = Rnd(0, 10)
y = Rnd(0, 10)
End
End

' We can add this class to myList as it extends MyClass
Class ExtendedClass Extends MyClass
Field z:Float
End[/monkeycode]


ziggy(Posted 2012) [#8]
@therevills: Nice example. Howeverm lists do not have any Length method. It should be Count.


Shinkiro1(Posted 2012) [#9]
Regarding lists, I was always interested in the Sort() functionality.
I know you can extend the List and the override the Compare() Method, but is it also possible to use a function-pointer for this.
In BMax you could do this:
[monkeycode]yourList.Sort (funcPointer)[/monkeycode]


therevills(Posted 2012) [#10]
@Ziggy, opps when I first type it out in Monk I got the error regarding Length and fixed it, I must have copied the old version. I've updated the post. Thanks!

@Shinkiro1, to do sorting with Monkey Lists you need to override the compare method: http://www.monkeycoder.co.nz/Community/post.php?topic=1257&post=11263

Or you can use the ArrayList in Diddy ;)