Stack<T>
Overview
Represents a last-in-first-out (LIFO) collection of objects.
Stack allows access only to top element of the collection. Stack accepts nil as a valid value for reference types and allows duplicate elements.
Note: On Nougat platform stack internally converts
nil to the NSNull type.Use case
You can use Stack<T> class when you need to create a LIFO queue:
var Numbers := new Stack<Integer>;
Numbers.Push(1);
Numbers.Push(2);
Numbers.Push(3);
Numbers.Push(4);
writeLn(Sugar.String.Format("Current: {0}", Numbers.Pop));
writeLn(Sugar.String.Format("Next on stack: {0}", Numbers.Peek));
while Numbers.Count > 0 do
writeLn(Sugar.String.Format("Current: {0}", Numbers.Pop));
Output:
Current: 4
Next on stack: 3
Current: 3
Current: 2
Current: 1
Loops
Please note: Instead of using for/each or for/in loops in the collections please use ForEach methods.
For example:
var Items := new List<String>(["One", "Two", "Three"]);
Items.ForEach(item -> DoSomething(item));
Location
- Reference: Sugar
- Namespace: Sugar.Collections
Clear mapped
method Clearvoid Clear()func Clear()void Clear()Sub Clear()Contains mapped
method Contains(Item: T): BooleanBoolean Contains(T Item)func Contains(_ Item: T) -> BooleanBoolean Contains(T Item)Function Contains(Item As T) As BooleanParameters:
- Item:
Count
property Count: Int32 read;Int32 Count { get; }var Count: Int32 { get{} }Int32 Count { __get; }ReadOnly Property Count() As Int32Peek mapped
method Peek: TT Peek()func Peek() -> TT Peek()Function Peek() As TPop mapped
method Pop: TT Pop()func Pop() -> TT Pop()Function Pop() As TPush mapped
method Push(Item: T)void Push(T Item)func Push(_ Item: T)void Push(T Item)Sub Push(Item As T)Parameters:
- Item:
ToArray mapped
method ToArray: array of TT[] ToArray()func ToArray() -> T...T[] ToArray()Function ToArray() As T()
Count
property Count: Int32 read;Int32 Count { get; }var Count: Int32 { get{} }Int32 Count { __get; }ReadOnly Property Count() As Int32
Clear mapped
method Clearvoid Clear()func Clear()void Clear()Sub Clear()Contains mapped
method Contains(Item: T): BooleanBoolean Contains(T Item)func Contains(_ Item: T) -> BooleanBoolean Contains(T Item)Function Contains(Item As T) As BooleanParameters:
- Item:
Peek mapped
method Peek: TT Peek()func Peek() -> TT Peek()Function Peek() As TPop mapped
method Pop: TT Pop()func Pop() -> TT Pop()Function Pop() As TPush mapped
method Push(Item: T)void Push(T Item)func Push(_ Item: T)void Push(T Item)Sub Push(Item As T)Parameters:
- Item:
ToArray mapped
method ToArray: array of TT[] ToArray()func ToArray() -> T...T[] ToArray()Function ToArray() As T()