Stack.Clone()方法用于创建堆栈的浅表副本。
语法:
Object Stack.Clone();
参数:无
返回值: Object –堆栈的浅表副本。
示例
declare and initialize a stack: Stack stk = new Stack(); insertting elements: stk.Push(100); stk.Push(200); stk.Push(300); stk.Push(400); stk.Push(500); creating clone/copy of the stack: Stack stk1 = (Stack)stk.Clone(); Output: stk: 500 400 300 200 100 stk1: 500 400 300 200 100
using System; using System.Text; using System.Collections; namespace Test { class Program { //打印堆栈元素的功能 static void printStack(Stack s) { foreach (Object obj in s) { Console.Write(obj + " "); } Console.WriteLine(); } static void Main(string[] args) { //声明并初始化堆栈 Stack stk = new Stack(); //插入元素 stk.Push(100); stk.Push(200); stk.Push(300); stk.Push(400); stk.Push(500); //打印堆栈元素 Console.WriteLine("Stack (stk) elements are..."); printStack(stk); //创建堆栈的克隆/副本 Stack stk1 = (Stack)stk.Clone(); //打印堆栈元素 Console.WriteLine("Stack (stk1) elements are..."); printStack(stk1); //按ENTER退出 Console.ReadLine(); } } }
输出结果
Stack (stk) elements are... 500 400 300 200 100 Stack (stk1) elements are... 500 400 300 200 100
参考:Stack.Clone方法