国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#使用Object類實(shí)現(xiàn)棧的方法詳解

2019-10-29 21:25:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了C#使用Object類實(shí)現(xiàn)棧的方法。分享給大家供大家參考,具體如下:

Stack類的代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 使用Object類實(shí)現(xiàn)后進(jìn)先出隊(duì)列{ class Stack {  private Object[] _items;  public Object[] Items  {   get { return this._items; }   set { this._items = value; }  }  //將對(duì)象壓入  public void Push(Object obj)  {   //第一次壓入時(shí),進(jìn)行初始化,長(zhǎng)度為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()  {   //為初始化或長(zhǎng)度為0時(shí),無(wú)法取出任何元素   if (this._items == null||this._items.Length == 0)    return null;   else   {    Object obj = this._items[this._items.Length - 1];    //刪除最后一個(gè)元素    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;  } }}

窗體檢測(cè)代碼:

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類實(shí)現(xiàn)后進(jìn)先出隊(duì)列{ 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類的時(shí)候形成很多不可控的資源占用,等待GC回收;

2.類型不安全,任何類型的數(shù)據(jù)都可以裝入object

3.可以設(shè)置Object數(shù)組的一個(gè)初始長(zhǎng)度,而不必每次壓入或者取出的時(shí)候都去臨時(shí)改變數(shù)組的長(zhǎng)度,具體做法是,通過(guò)Stack的構(gòu)造函數(shù)生成一個(gè)指定長(zhǎng)度的數(shù)組,在壓入和取出的時(shí)候,并不對(duì)這個(gè)初始化的長(zhǎng)度進(jìn)行調(diào)整,而只是用一個(gè)int數(shù)值intPoint記錄目前所擁有的值的位置,對(duì)已經(jīng)取出的object,實(shí)際并沒有把它刪除,只是不去管它而已。這樣做的好處是,一次設(shè)定數(shù)組長(zhǎng)度,使用一個(gè)類似指針的東西定位“有效”元素,這種方法更可取。

實(shí)際上,.net2.0以上提供了Stack<>泛型類可以直接完成棧,使用非常方便,而且避免了強(qiáng)制類型轉(zhuǎn)換帶來(lái)的損耗,實(shí)現(xiàn)了類型安全。第二段代碼中已經(jīng)給出使用方式,非常簡(jiǎn)單。

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

 


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到c#教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 顺平县| 成安县| 礼泉县| 剑河县| 丽水市| 海林市| 黄陵县| 樟树市| 松原市| 绍兴市| 濉溪县| 合肥市| 瑞金市| 吴堡县| 鄂尔多斯市| 漾濞| 华容县| 咸宁市| 新乡县| 武宁县| 京山县| 崇明县| 柳林县| 涞源县| 灌阳县| 滕州市| 资源县| 永寿县| 濉溪县| 增城市| 兴山县| 定边县| 抚远县| 信阳市| 望谟县| 焦作市| 大同市| 崇仁县| 敖汉旗| 天门市| 泾阳县|