How do lists work?

Monkey Forums/Monkey Programming/How do lists work?

OvineByDesign(Posted 2011) [#1]
Hello,

How do lists work in Monkey? I see lists for Strings, and Int, but can we have lists of Object Instance references?


Warpy(Posted 2011) [#2]
Dealing with objects and the new generics feature mean the syntax has changed quite a bit. Here's an example

Class Dude           'declare a new type
  Field name$

  Method New(_name$)  'define the constructor function. It can take arguments now!
    name = _name
  End
End

Local dudes:List<Dude>           'create a new list, which takes Dude objects
dudes.AddLast (New Dude("Jim"))  'add a new Dude to the list.

For Local dude:= EachIn dudes    'You must put Local/Global before variable declarations now, but you can declare type implicitly with the := operator
  Print dude.name
Next



BlackSp1der(Posted 2011) [#3]
anyone knows a way to get the values of the list from bottom to top?
something like:
for local i = list.Count()-1 to 0 step -1
list.elementAt(i)
end



Beaker(Posted 2011) [#4]
You could make a temp List and copy the items into it using AddFirst().

Or use a Stack.


Difference(Posted 2011) [#5]
It would be nice to have a List.Reverse() and List.Reversed() like in BlitzMax