C#中将堆栈复制到数组
要将堆栈复制到数组,代码如下:
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Stack<int> stack = new Stack<int>();
stack.Push(10);
stack.Push(20);
stack.Push(30);
stack.Push(40);
stack.Push(50);
stack.Push(60);
stack.Push(70);
stack.Push(80);
stack.Push(90);
stack.Push(100);
Console.WriteLine("Displaying the stack...");
foreach(int val in stack){
Console.WriteLine(val);
}
int[] intArr = new int[stack.Count];
stack.CopyTo(intArr, 0);
Console.WriteLine("Displaying the array...");
foreach(int val in intArr){
Console.WriteLine(val);
}
}
}输出结果
这将产生以下输出-
Displaying the stack... 100 90 80 70 60 50 40 30 20 10 Displaying the array... 100 90 80 70 60 50 40 30 20 10
示例
现在让我们来看另一个示例-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Stack<int> stack = new Stack<int>();
stack.Push(10);
stack.Push(20);
stack.Push(30);
stack.Push(40);
stack.Push(50);
Console.WriteLine("Displaying the stack...");
foreach(int val in stack){
Console.WriteLine(val);
}
int[] intArr = new int[10];
stack.CopyTo(intArr, 2);
Console.WriteLine("Displaying the array...");
foreach(int val in intArr){
Console.WriteLine(val);
}
}
}输出结果
这将产生以下输出-
Displaying the stack... 50 40 30 20 10 Displaying the array... 0 0 50 40 30 20 10 0 0 0
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短