亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
C# 中的棧函數(shù)
C# 堆棧中的方法
示例#1
Example #2

C# 堆棧

Sep 03, 2024 pm 03:30 PM
c# c# tutorial

以后進(jìn)先出表示的對(duì)象集合稱為堆棧,它是一個(gè)根據(jù)程序的需要隨著向堆棧中添加元素而增加的集合,因此它是一個(gè)動(dòng)態(tài)集合棧中可以存儲(chǔ)相同類型和不同類型的元素,向棧中添加元素的過程稱為將元素壓入棧,從棧中移除元素的過程稱為將元素從棧中彈出這個(gè)堆棧屬于 Systems。集合命名空間。

語法:

C# Stack 的語法如下:

Stack stack_name = new Stack();

其中 stack_name 是 stack.l 的名稱

C# 中的棧函數(shù)

  • 每當(dāng)我們需要以后進(jìn)先出的順序訪問堆棧中的元素時(shí),我們就創(chuàng)建一個(gè)名為 Stack 的對(duì)象集合。
  • 向 Stack 添加元素的過程稱為將元素壓入堆棧,從堆棧中移除元素的過程稱為從 Stack 彈出元素。
  • 堆棧是元素的動(dòng)態(tài)集合,因?yàn)槎褩5拇笮‰S著向堆棧中添加元素而增加。
  • 堆??梢匀菁{的元素?cái)?shù)量稱為堆棧的容量。隨著棧的大小隨著向棧中添加元素而增加,棧的容量也會(huì)通過重新分配而增加。
  • 堆棧中允許存在重復(fù)元素。
  • 堆棧接受 Null 作為類型、引用的有效值。

C#中Stack有多個(gè)構(gòu)造函數(shù)。他們是:

  • Stack(): 棧類的一個(gè)新實(shí)例被初始化,該實(shí)例為空,初始容量為默認(rèn)值。
  • Stack(ICollection):初始化 stack 類的新實(shí)例,該實(shí)例由從指定為參數(shù)的集合中取出的元素組成,初始容量與取出的元素?cái)?shù)量相同來自指定為參數(shù)的集合。
  • Stack(Int32): 棧類的一個(gè)新實(shí)例被初始化,該實(shí)例為空,其初始容量可以是參數(shù)指定的初始容量,也可以是默認(rèn)的初始容量。

C# 堆棧中的方法

C#中Stack有多種方法。他們是:

  • Clear():使用 Clear() 方法刪除堆棧中的對(duì)象。
  • Push(Object):使用 Push(Object) 方法將指定為參數(shù)的對(duì)象插入到堆棧頂部。
  • Contains(Object):Contains(Object) 方法用于確定堆棧中是否存在元素。
  • Peek():返回堆棧頂部指定的對(duì)象,但不會(huì)使用 Peek() 方法刪除。
  • Pop():返回堆棧頂部指定的對(duì)象,并使用 Pop() 方法將其刪除。

示例

以下是c#堆棧的示例:

示例#1

考慮下面的示例程序來演示 Push() 方法、Pop() 方法、Peek() 方法、Contains() 方法和 Clear() 方法:

代碼:

using System;
using System.Collections;
//a class called program is defined
class program
{
//main method is called
public static void Main()
{
//a new stack is created
Stack mystk = new Stack();
//Adding the elements to the newly created stack
mystk.Push("India");
mystk.Push("USA");
mystk.Push("Canada");
mystk.Push("Germany");
//displaying the elements of the stack using foreach loop
Console.Write("The elements in the Stack are : ");
foreach(varele in mystk)
{
Console.WriteLine(ele);
}
//using contains() method to check if an element is present in the stack or not
Console.WriteLine(mystk.Contains("Germany"));
// The count of the elements in the stack is displayed
Console.Write("The count of elements in the Stack are : ");
Console.WriteLine(mystk.Count);
// displaying the top most element of the stack using Peek() method
Console.WriteLine("The topmost element in the stack is? : " + mystk.Peek());
//Using pop() method to remove the top element in the stack
Console.WriteLine("the element of the stack that is going to be removed" + " is: {0}",mystk.Pop());
Console.Write("The elements in the Stack after using pop() method are : ");
foreach(var el in mystk)
{
Console.WriteLine(el);
}
Console.Write("The count of elements in the Stack after using pop() method is : ");
Console.WriteLine(mystk.Count);
//using Clear() method to remove all the elements in the stack
mystk.Clear();
Console.Write("The count of elements in the Stack after using Clear() method is : ");
Console.WriteLine(mystk.Count);
}
}

輸出:

C# 堆棧

在上面的程序中,定義了一個(gè)名為program的類。然后調(diào)用main方法。然后創(chuàng)建一個(gè)新的堆棧。然后使用 Push() 方法將元素添加到新創(chuàng)建的堆棧中。然后使用 foreach 循環(huán)顯示新創(chuàng)建的堆棧的元素。然后 contains() 方法用于檢查堆棧中是否存在元素。然后使用 count() 方法顯示堆棧中元素的數(shù)量。然后使用 Peek() 方法顯示堆棧最頂層的元素。然后使用 Pop() 方法刪除堆棧最頂層的元素。然后使用 Pop() 方法后再次顯示元素的計(jì)數(shù)和堆棧的元素。然后使用Clear()方法刪除堆棧中的所有元素。然后在使用 Clear() 方法后再次顯示元素的計(jì)數(shù)和堆棧的元素。程序的輸出如上面的快照所示。

  • Clone(): A shallow copy of the stack is created using the Clone() method.
  • Equals(Object): The Equals(Object) method is used to determine if the object specified as the parameter is equal to the current object.
  • Synchronized(Stack): A synchronized wrapper for the stack is returned using the Synchronized(Stack) method.
  • CopyTo(Array,Int32): The stack is copied into an array which is one dimensional with the index of the array specified as a parameter.
  • ToArray(): The stack is copied to a new array using ToArray() method.
  • GetType(): The type of the current instance is obtained using GetType() method.
  • ToString(): A string representing the current object is returned using ToString() method.
  • GetEnumerator(): An IEnumerator for the stack is returned using GetEnumerator() method.
  • GetHashCode(): The GetHashCode() method is the hash function by default.
  • MemberwiseClone(): A shallow copy of the current object is created using MemberwiseClone() method.

Example #2

Consider the example program below to demonstrate Clone() method, Equals() method, Synchronized() method, CopyTo() method, ToArray() method, GetType() method and GetEnumerator() method:

Code:

using System;
using System.Collections;
//a class called program is defined
class program
{
// Main Method is called
public static void Main(string[] args)
{
// creating a new stack
Stack mystk = new Stack();
mystk.Push("India");
mystk.Push("USA");
mystk.Push("Canada");
mystk.Push("Germany");
Console.Write("The elements in the Stack are : ");
foreach(varele in mystk)
{
Console.WriteLine(ele);
}
// a clone of the newly created stack is created
Stack mystk1 = (Stack)mystk.Clone();
// the top most element of the clone of the newly created stack is removed using pop() method
mystk1.Pop();
Console.Write("The elements in the clone of the Stack after using pop() method are : ");
//the elements of the clone of the newly created stack is displayed
foreach(Object ob in mystk1)
Console.WriteLine(ob);
//checking if the elements of the clone of the newly created stack and the newly created stack are equal or not
Console.Write("The elements in the clone of the Stack and the stack are equal or not : ");
Console.WriteLine(mystk.Equals(mystk1));
//Checking if the clone of the newly created stack and the newly created stack is synchronised or not
Console.WriteLine("The Clone of the newly created stack is {0}.", mystk1.IsSynchronized ? "Synchronized" : "Not Synchronized");
Console.WriteLine("The newly created stack is {0}.", mystk.IsSynchronized ? "Synchronized" : "Not Synchronized");
//a new array of strings is created and the newly created stack is assigned to this array
string[] arra = new string[mystk.Count];
// The elements of the newly created stack is copied to the array
mystk.CopyTo(arra, 0);
// the elements of the array are displayed
Console.Write("The elements of the array copied from the newly created stack are : ");
foreach(string st in arra)
{
Console.WriteLine(st);
}
//converting the elements of the newly created stack to array using toarray() method
Object[] ar1 = mystk.ToArray();
Console.Write("The elements of the array copied from the newly created stack by using ToArray() method are :");
//the elements of the array are displayed
foreach(Object st1 in ar1)
{
Console.WriteLine(st1);
}
Console.WriteLine("The type of newly created stack before using "+
"ToStringMethod is: "+mystk.GetType());
Console.WriteLine("The type of newly created stack after using "+
"ToString Method is: "+mystk.ToString().GetType());
Console.Write("The elements of the newly created stack after using GetEnumerator() method are : ");
//Getenumerator() method is used to obtain the enumerator of thestack
IEnumeratorenume = mystk.GetEnumerator();
while (enume.MoveNext())
{
Console.WriteLine(enume.Current);
}
}
}

Output:

C# 堆棧

In the above program, a class called program is defined. Then the main method is called. Then a new stack is created. Then the clone of the newly created stack is created by using the clone() method. Then the topmost element of the clone of the newly created stack is removed using the pop() method. Then the elements of the clone of the newly created method are displayed. Then Equals() method is used to check if the newly created stack and the clone of the newly created stack are equal or not. Then the synchronized() method is used to check if the newly created stack and the clone of the newly created stack are synchronized or not. Then Copyto() method is used to copy the newly created stack to an array and the elements of the array are displayed. Then ToArray() method is used to copy the newly created stack to another array and then the elements of the array are displayed. Then GetType() method is used to obtain the type of the newly created stack. Then ToString() method is used to convert the type stack to string. Then GetEnumerator() method is used to obtain the IEnumerator of the stack. The output of the program is shown in the snapshot above.

以上是C# 堆棧的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

c#多線程和異步的區(qū)別 c#多線程和異步的區(qū)別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區(qū)別在于,多線程同時(shí)執(zhí)行多個(gè)線程,而異步在不阻塞當(dāng)前線程的情況下執(zhí)行操作。多線程用于計(jì)算密集型任務(wù),而異步用于用戶交互操作。多線程的優(yōu)勢(shì)是提高計(jì)算性能,異步的優(yōu)勢(shì)是不阻塞 UI 線程。選擇多線程還是異步取決于任務(wù)性質(zhì):計(jì)算密集型任務(wù)使用多線程,與外部資源交互且需要保持 UI 響應(yīng)的任務(wù)使用異步。

C#與C:歷史,進(jìn)化和未來前景 C#與C:歷史,進(jìn)化和未來前景 Apr 19, 2025 am 12:07 AM

C#和C 的歷史與演變各有特色,未來前景也不同。1.C 由BjarneStroustrup在1983年發(fā)明,旨在將面向?qū)ο缶幊桃隒語言,其演變歷程包括多次標(biāo)準(zhǔn)化,如C 11引入auto關(guān)鍵字和lambda表達(dá)式,C 20引入概念和協(xié)程,未來將專注于性能和系統(tǒng)級(jí)編程。2.C#由微軟在2000年發(fā)布,結(jié)合C 和Java的優(yōu)點(diǎn),其演變注重簡(jiǎn)潔性和生產(chǎn)力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注于開發(fā)者的生產(chǎn)力和云計(jì)算。

xml怎么改格式 xml怎么改格式 Apr 03, 2025 am 08:42 AM

可以采用多種方法修改 XML 格式:使用文本編輯器(如 Notepad )進(jìn)行手工編輯;使用在線或桌面 XML 格式化工具(如 XMLbeautifier)進(jìn)行自動(dòng)格式化;使用 XML 轉(zhuǎn)換工具(如 XSLT)定義轉(zhuǎn)換規(guī)則;或者使用編程語言(如 Python)進(jìn)行解析和操作。修改時(shí)需謹(jǐn)慎,并備份原始文件。

xml如何轉(zhuǎn)化為word xml如何轉(zhuǎn)化為word Apr 03, 2025 am 08:15 AM

有三種將 XML 轉(zhuǎn)換為 Word 的方法:使用 Microsoft Word、使用 XML 轉(zhuǎn)換器或使用編程語言。

xml怎么轉(zhuǎn)換成json xml怎么轉(zhuǎn)換成json Apr 03, 2025 am 09:09 AM

將 XML 轉(zhuǎn)換為 JSON 的方法包括:使用編程語言(如 Python、Java、C#)編寫腳本或程序進(jìn)行轉(zhuǎn)換;使用在線工具(如 XML 轉(zhuǎn)換為 JSON、Gojko's XML 轉(zhuǎn)換器、XML 在線工具)粘貼或上傳 XML 數(shù)據(jù)并選擇 JSON 格式輸出;使用 XML 到 JSON 轉(zhuǎn)換器(如 Oxygen XML Editor、Stylus Studio、Altova XMLSpy)執(zhí)行轉(zhuǎn)換任務(wù);使用 XSLT 樣式表將 XML 轉(zhuǎn)換為 JSON;使用數(shù)據(jù)集成工具(如 Informatic

c#多線程編程是什么  c#多線程編程用處 c#多線程編程是什么 c#多線程編程用處 Apr 03, 2025 pm 02:45 PM

C# 多線程編程是一種讓程序同時(shí)執(zhí)行多項(xiàng)任務(wù)的技術(shù),它可以通過提升性能、提高響應(yīng)能力和實(shí)現(xiàn)并行處理來提高程序效率。雖然 Thread 類提供了直接創(chuàng)建線程的方法,但 Task 和 async/await 等高級(jí)工具可以提供更安全的異步操作和更簡(jiǎn)潔的代碼結(jié)構(gòu)。多線程編程中常見的難題包括死鎖、競(jìng)態(tài)條件和資源泄漏,需要仔細(xì)設(shè)計(jì)線程模型和使用適當(dāng)?shù)耐綑C(jī)制來避免這些問題。

C#.NET:使用.NET生態(tài)系統(tǒng)構(gòu)建應(yīng)用程序 C#.NET:使用.NET生態(tài)系統(tǒng)構(gòu)建應(yīng)用程序 Apr 27, 2025 am 12:12 AM

如何利用.NET構(gòu)建應(yīng)用?使用.NET構(gòu)建應(yīng)用可以通過以下步驟實(shí)現(xiàn):1)了解.NET基礎(chǔ)知識(shí),包括C#語言和跨平臺(tái)開發(fā)支持;2)學(xué)習(xí)核心概念,如.NET生態(tài)系統(tǒng)的組件和工作原理;3)掌握基本和高級(jí)用法,從簡(jiǎn)單控制臺(tái)應(yīng)用到復(fù)雜的WebAPI和數(shù)據(jù)庫(kù)操作;4)熟悉常見錯(cuò)誤與調(diào)試技巧,如配置和數(shù)據(jù)庫(kù)連接問題;5)應(yīng)用性能優(yōu)化與最佳實(shí)踐,如異步編程和緩存。

從網(wǎng)絡(luò)到桌面:C#.NET的多功能性 從網(wǎng)絡(luò)到桌面:C#.NET的多功能性 Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

See all articles