130.C# Stack 堆栈_c# 堆栈分析
摘要
堆栈(Stack)代表了一个后进先出的对象集合。当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素。
正文
属性
属性 | 描述 |
Count | 获取 Stack 中包含的元素个数。 |
方法
序号 | 方法名 & 描述 |
1 | **public virtual void Clear();**从 Stack 中移除所有的元素。 |
2 | **public virtual bool Contains( object obj );**判断某个元素是否在 Stack 中。 |
3 | **public virtual object Peek();**返回在 Stack 的顶部的对象,但不移除它。 |
4 | **public virtual object Pop();**移除并返回在 Stack 的顶部的对象。 |
5 | **public virtual void Push( object obj );**向 Stack 的顶部添加一个对象。 |
6 | **public virtual object[] ToArray();**复制 Stack 到一个新的数组中。 |
一个例子
添加
Stack<int> st = new Stack<int>();
private void btnPush_Click(object sender, EventArgs e)
{
Random random = new Random();
for (int i = 0; i < 10; i++)
{
st.Push(random.Next(1, 100));
}
ForItems();
}删除
private void btnPop_Click(object sender, EventArgs e)
{
var ret= st.Pop();
MessageBox.Show(ret.ToString());
ForItems();
}遍历
private void ForItems()
{
lstAll.Items.Clear();
foreach (var item in st)
{
lstAll.Items.Add(item);
}
} 相关文章
- Spring Boot中对接Twilio以实现发送验证码和验证短信码
- Spring Boot 3.5:这次更新让你连配置都不用写了,惊不惊喜?
- Spring Boot+Pinot实战:毫秒级实时竞价系统构建
- SpringBoot敏感配置项加密与解密实战
- SpringBoot 注解最全详解,建议收藏!
- Spring Boot 常用注解大全:从入门到进阶
- SpringBoot启动之谜:@SpringBootApplication如何让配置化繁为简
- Springboot集成Kafka原理_spring集成kafka的原理
- Spring Boot中@Data注解的深度解析与实战应用
- 大佬用1000字就把SpringBoot的配置文件讲的明明白白!
