C#使用Object類實現(xiàn)棧的方法詳解
本文實例講述了C#使用Object類實現(xiàn)棧的方法。分享給大家供大家參考,具體如下:
Stack類的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 使用Object類實現(xiàn)后進(jìn)先出隊列
{
class Stack
{
private Object[] _items;
public Object[] Items
{
get { return this._items; }
set { this._items = value; }
}
//將對象壓入
public void Push(Object obj)
{
//第一次壓入時,進(jìn)行初始化,長度為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類實現(xiàn)后進(jìn)先出隊列
{
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.類型不安全,任何類型的數(shù)據(jù)都可以裝入object
3.可以設(shè)置Object數(shù)組的一個初始長度,而不必每次壓入或者取出的時候都去臨時改變數(shù)組的長度,具體做法是,通過Stack的構(gòu)造函數(shù)生成一個指定長度的數(shù)組,在壓入和取出的時候,并不對這個初始化的長度進(jìn)行調(diào)整,而只是用一個int數(shù)值intPoint記錄目前所擁有的值的位置,對已經(jīng)取出的object,實際并沒有把它刪除,只是不去管它而已。這樣做的好處是,一次設(shè)定數(shù)組長度,使用一個類似指針的東西定位“有效”元素,這種方法更可取。
實際上,.net2.0以上提供了Stack<>泛型類可以直接完成棧,使用非常方便,而且避免了強(qiáng)制類型轉(zhuǎn)換帶來的損耗,實現(xiàn)了類型安全。第二段代碼中已經(jīng)給出使用方式,非常簡單。
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#遍歷算法與技巧總結(jié)》、《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
C#實現(xiàn)根據(jù)實體類自動創(chuàng)建數(shù)據(jù)庫表
本文主要介紹了C#通過自定義特性實現(xiàn)根據(jù)實體類自動創(chuàng)建數(shù)據(jù)庫表的方法。具有很好的參考價值,需要的朋友一起來看下吧2016-12-12
C#中把Datatable轉(zhuǎn)換為Json的5個代碼實例
這篇文章主要介紹了C#中把Datatable轉(zhuǎn)換為Json的5個代碼實例,需要的朋友可以參考下2014-04-04
c#winform窗口頁面一打開就加載的實現(xiàn)方式
這篇文章主要介紹了c#winform窗口頁面一打開就加載的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
C#使用StopWatch獲取程序毫秒級執(zhí)行時間的方法
這篇文章主要介紹了C#使用StopWatch獲取程序毫秒級執(zhí)行時間的方法,涉及C#操作時間的相關(guān)技巧,需要的朋友可以參考下2015-04-04
C#中Write()和WriteLine()的區(qū)別分析
這篇文章主要介紹了C#中Write()和WriteLine()的區(qū)別分析,需要的朋友可以參考下2020-11-11

