C#使用Object类实现栈的方法详解

作者:丛晓男 时间:2021-08-03 17:36:16 

本文实例讲述了C#使用Object类实现栈的方法。分享给大家供大家参考,具体如下:

Stack类的代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 使用Object类实现后进先出队列
{
class Stack
{
 private Object[] _items;
 public Object[] Items
 {
  get { return this._items; }
  set { this._items = value; }
 }
 //将对象压入
 public void Push(Object obj)
 {
  //第一次压入时,进行初始化,长度为1
  if (this._items == null)
  {
   this._items = new Object[1];
   this._items[0] = obj;
  }
  else
  {
   int count = this._items.Length;
   Object[] objTemp = this._items;
   this._items = new Object[count + 1];
   int i = 0;
   foreach (Object o in objTemp)
   {
    this._items[i++] = o;
   }
   this._items[i] = obj;
  }
 }
 //按后入先出取出
 public Object Pop()
 {
  //为初始化或长度为0时,无法取出任何元素
  if (this._items == null||this._items.Length == 0)
   return null;
  else
  {
   Object obj = this._items[this._items.Length - 1];
   //删除最后一个元素
   this.DeleteLastObj();
   return obj;
  }
 }
 private void DeleteLastObj()
 {
  Object[] objTemp = new Object[this._items.Length - 1];
  for (int i = 0; i < this._items.Length - 1; i++)
  {
   objTemp[i] = this._items[i];
  }
  this._items = objTemp;
 }
}
}

窗体检测代码:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 使用Object类实现后进先出队列
{
public partial class Form1 : Form
{
 public Form1()
 {
  InitializeComponent();
 }
 private Stack stack = new Stack();
 private Stack<string> stackGeneric= new Stack<string>();
 private void button1_Click(object sender, EventArgs e)
 {
  stack.Push(this.textBox1.Text);
 }
 private void button2_Click(object sender, EventArgs e)
 {
  Object[] objs = stack.Items;
  foreach(Object o in objs)
  {
   Console.WriteLine(o.ToString());
  }
 }
 private void button1_Click_1(object sender, EventArgs e)
 {
  try
  {
   Console.WriteLine(this.stack.Pop().ToString());
  }
  catch
  {
   Console.WriteLine("null");
  }
 }
 private void button3_Click(object sender, EventArgs e)
 {
  this.stackGeneric.Push(this.textBox2.Text);
 }
 private void button4_Click(object sender, EventArgs e)
 {
  try
  {
   Console.WriteLine(this.stackGeneric.Pop());
  }
  catch (InvalidOperationException)
  {
   Console.WriteLine("null");
  }
 }
}
}

1.使用Stack类的时候形成很多不可控的资源占用,等待GC回收;

2.类型不安全,任何类型的数据都可以装入object

3.可以设置Object数组的一个初始长度,而不必每次压入或者取出的时候都去临时改变数组的长度,具体做法是,通过Stack的构造函数生成一个指定长度的数组,在压入和取出的时候,并不对这个初始化的长度进行调整,而只是用一个int数值intPoint记录目前所拥有的值的位置,对已经取出的object,实际并没有把它删除,只是不去管它而已。这样做的好处是,一次设定数组长度,使用一个类似指针的东西定位“有效”元素,这种方法更可取。

实际上,.net2.0以上提供了Stack<>泛型类可以直接完成栈,使用非常方便,而且避免了强制类型转换带来的损耗,实现了类型安全。第二段代码中已经给出使用方式,非常简单。

希望本文所述对大家C#程序设计有所帮助。

标签:C#,栈
0
投稿

猜你喜欢

  • java实现udp通讯的代码

    2022-07-18 13:28:52
  • 解决RestTemplate加@Autowired注入不了的问题

    2022-07-14 03:00:48
  • WPF实现窗体中的悬浮按钮

    2022-07-25 17:45:53
  • Java编程用两个栈实现队列代码分享

    2023-03-22 01:05:05
  • Android调用系统摄像头拍照并显示在ImageView上

    2022-02-19 17:42:00
  • android 获取上一个activity返回值的方法

    2023-06-15 13:22:22
  • C#使用LOCK实现线程同步

    2022-02-02 10:51:14
  • Centos中安装jdk案例讲解

    2023-04-30 00:37:50
  • Android仿百度福袋红包界面

    2023-11-01 04:53:01
  • 详解Spring Boot Oauth2缓存UserDetails到Ehcache

    2023-02-26 21:57:12
  • 使用linq to xml修改app.config示例(linq读取xml)

    2022-11-22 22:01:28
  • Java定时调用.ktr文件的示例代码(解决方案)

    2021-12-29 13:21:49
  • java状态机方案解决订单状态扭转示例详解

    2022-01-10 02:55:43
  • Android实现异步加载图片

    2021-08-14 16:44:20
  • Android简单使用PopupWindow的方法

    2023-09-13 19:51:22
  • Mybatis MapperScannerConfigurer自动扫描Mapper接口生成代理注入到Spring的方法

    2023-04-17 11:57:25
  • Flutter集成到已有iOS工程的方法步骤

    2023-07-31 22:53:53
  • Android中Notification用法实例总结

    2023-03-16 23:05:38
  • C#遍历得到checkboxlist选中值和设置选中项的代码

    2022-10-25 20:52:22
  • EditText监听方法,实时的判断输入多少字符

    2021-08-16 11:32:36
  • asp之家 软件编程 m.aspxhome.com