Stack的本质
Stack(栈)是一个C#为我们封装好的类,它的本质也是object[]数组,只是封装了特殊的存储规则。
Stack是栈存储容器,栈是一种先进后出的数据结构。
先存入的数据后获取,后存入的数据先获取。
栈是先进后出。
申明
//需要引用命名空间 System.Collections
Stack stack = new Stack();
增取查改
增
//压栈
stack.Push(1);
stack.Push("123");
stack.Push(true);
stack.Push(1.2f);
stack.Push(new Test());
取
//栈中不存在删除的概念
//只有取的概念
//弹栈
object v = stack.Pop();
Console.WriteLine(v);
v = stack.Pop();
Console.WriteLine(v);
查
//1.栈无法查看指定位置的 元素
// 只能查看栈顶的内容
v = stack.Peek();
Console.WriteLine(v);
v = stack.Peek();
Console.WriteLine(v);
//2.查看元素是否存在于栈中
if( stack.Contains("123") )
{
Console.WriteLine("存在123");
}
改
//栈无法改变其中的元素 只能压(存)和弹(取)
//实在要改 只有清空
stack.Clear();
Console.WriteLine(stack.Count);
stack.Push("1");
stack.Push(2);
stack.Push("哈哈哈");
遍历
//1.长度
Console.WriteLine(stack.Count);
//2.用foreach遍历
// 而且遍历出来的顺序 也是从栈顶到栈底
foreach(object item in stack)
{
Console.WriteLine(item);
}
//3.还有一种遍历方式
// 将栈转换为object数组
// 遍历出来的顺序 也是从栈顶到栈底
object[] array = stack.ToArray();
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
Console.WriteLine(stack.Count);
//4.循环弹栈
while( stack.Count > 0 )
{
object o = stack.Pop();
Console.WriteLine(o);
}
Console.WriteLine(stack.Count);
装箱拆箱
由于用万物之父来存储数据,自然存在装箱拆箱。
当往其中进行值类型存储时就是在装箱。
当将值类型对象取出来转换使用时,就存在拆箱。