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

来源:互联网 发布:外卖小票打印软件 编辑:程序博客网 时间:2024/06/05 15:34

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

Stack类的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace使用Object类实现后进先出队列
{
 classStack
 {
  privateObject[] _items;
  publicObject[] Items
  {
   get{ return this._items; }
   set{ this._items = value; }
  }
  //将对象压入
  publicvoid Push(Object obj)
  {
   //第一次压入时,进行初始化,长度为1
   if(this._items ==null)
   {
    this._items =new Object[1];
    this._items[0] = obj;
   }
   else
   {
    intcount = this._items.Length;
    Object[] objTemp =this._items;
    this._items =new Object[count + 1];
    inti = 0;
    foreach(Object o inobjTemp)
    {
     this._items[i++] = o;
    }
    this._items[i] = obj;
   }
  }
  //按后入先出取出
  publicObject Pop()
  {
   //为初始化或长度为0时,无法取出任何元素
   if(this._items ==null||this._items.Length == 0)
    returnnull;
   else
   {
    Object obj =this._items[this._items.Length - 1];
    //删除最后一个元素
    this.DeleteLastObj();
    returnobj;
   }
  }
  privatevoid 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;
  }
 }
}

窗体检测代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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类实现后进先出队列
{
 publicpartial classForm1 : Form
 {
  publicForm1()
  {
   InitializeComponent();
  }
  privateStack stack = newStack();
  privateStack<string> stackGeneric=new Stack<string>();
  privatevoid button1_Click(objectsender, EventArgs e)
  {
   stack.Push(this.textBox1.Text);
  }
  privatevoid button2_Click(objectsender, EventArgs e)
  {
   Object[] objs = stack.Items;
   foreach(Object oin objs)
   {
    Console.WriteLine(o.ToString());
   }
  }
  privatevoid button1_Click_1(objectsender, EventArgs e)
  {
   try
   {
    Console.WriteLine(this.stack.Pop().ToString());
   }
   catch
   {
    Console.WriteLine("null");
   }
  }
  privatevoid button3_Click(objectsender, EventArgs e)
  {
   this.stackGeneric.Push(this.textBox2.Text);
  }
  privatevoid button4_Click(objectsender, 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<>泛型类可以直接完成栈,使用非常方便,而且避免了强制类型转换带来的损耗,实现了类型安全。第二段代码中已经给出使用方式,非常简单
原创粉丝点击