[学习官方例子]TStack
代码如下:
program TStackTest; {$APPTYPE CONSOLE} uses SysUtils, Generics.Collections; var Stack: TStack<String>; begin Stack := TStack<String>.Create(); Stack.Push('John'); Stack.Push('Mary'); Stack.Push('Bob'); Stack.Push('Anna'); Stack.Push('Erica'); writeln('Last pushed element is: "' + Stack.Peek() + '".'); writeln('The stack contains '+IntToStr(Stack.Count)+' elements.'); Stack.Extract(); //出去顶元素 Stack.TrimExcess(); //设置容量为当前的元素个数 writeln('The stack contains '+IntToStr(Stack.Count)+' elements.'); writeln('Last pushed element is: "'+Stack.Pop()+'".'); Stack.Clear; Stack.Free; readln; end.
效果图: