Monkey2 – Stack – simple example

Namespace myapp

#Import "<std>"
Using std.collections
Using std.stringio

Function Main()
Local stk:=New Stack<String>
stk.Push("Hallo world")
stk.Push("Another line")

For Local s:String=Eachin stk
Print s
Next
Print "Capacity : "+stk.Capacity
Print "Size     : "+stk.Length
stk.Compact()
Print "Capacity : "+stk.Capacity
Print "Size     : "+stk.Length
Print "Item # 1 : "+stk.Get(1) ' counting from 0...
print "Item # 4 : "+stk.Get(4) ' get an error ... of course!

'change an item in the stack
stk.Set(0,"Good Morning!")
Print stk.Get(0) 'now print Good Morning! instead of Hallo World

End

Just to learn something new (Capacity, Length and Compact() etc !

Stacks are much better than simple arrays: everything you need when you are using an array, without the problems of the array! (I really miss this feature in BlitzMax !)

Basic example, but needed for knowing better Monkey2!

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close