国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目次
C# におけるスタックの関數(shù)
C# スタックのメソッド
例 #1
Example #2

C# スタック

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

後入れ先出しで表されるオブジェクトのコレクションはスタックと呼ばれ、プログラムの必要に応じてスタックに要素が追加されることで増加するコレクションであるため、動的コレクションとなります。同じ型と異なる型の両方の要素をスタックに格納できます。要素をスタックに追加するプロセスは要素をスタックにプッシュすると呼ばれ、要素をスタックから削除するプロセスはスタックから要素をポップすると呼ばれます。このスタックはシステムの下にあります。コレクションの名前空間。

構(gòu)文:

C# スタックの構(gòu)文は次のとおりです:

Stack stack_name = new Stack();

ここで、stack_name は stack.l の名前です

C# におけるスタックの関數(shù)

  • 後入れ先出しの順序でスタックの要素にアクセスする必要がある場合は、Stack と呼ばれるオブジェクトのコレクションを使用します。
  • スタックに要素を追加するプロセスは、要素をスタックにプッシュすると呼ばれ、スタックから要素を削除するプロセスは、スタックから要素をポップすると呼ばれます。
  • スタックに要素を追加するとスタックのサイズが増加するため、スタックは要素の動的なコレクションです。
  • スタックが保持できる要素の數(shù)は、スタックの容量と呼ばれます。スタックに要素が追加されるとスタックのサイズが増加するため、再割り當(dāng)てによってスタックの容量も増加します。
  • スタック內(nèi)では重複した要素が許可される可能性があります。
  • Null は、型および參照の有効な値としてスタックによって受け入れられます。

C# のスタックにはいくつかのコンストラクターがあります。それらは次のとおりです:

  • Stack(): スタック クラスの新しいインスタンスが初期化されます。これは空であり、初期容量はデフォルトです。
  • Stack(ICollection): パラメータとして指定されたコレクションから取得された要素で構(gòu)成されるスタック クラスの新しいインスタンスが初期化され、初期容量は取得された要素の數(shù)と同じになります。パラメータとして指定されたコレクションから。
  • Stack(Int32): 空のスタック クラスの新しいインスタンスが初期化され、その初期容量はパラメーターとして指定された初期容量またはデフォルトの初期容量のいずれかになります。

C# スタックのメソッド

C# のスタックにはいくつかのメソッドがあります。それらは次のとおりです:

  • Clear(): スタックのオブジェクトは Clear() メソッドを使用して削除されます。
  • Push(Object): パラメータとして指定されたオブジェクトは、Push(Object) メソッドを使用してスタックの先頭に挿入されます。
  • Contains(Object): Contains(Object) メソッドは、要素がスタックに存在するかどうかを判斷するために使用されます。
  • Peek(): スタックの最上位に指定されたオブジェクトは返されますが、Peek() メソッドを使用して削除されません。
  • Pop(): スタックの先頭で指定されたオブジェクトが返され、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# スタック

上記のプログラムでは、programというクラスが定義されています。次に、main メソッドが呼び出されます。次に、新しいスタックが作成されます。次に、Push() メソッドを使用して、要素が新しく作成されたスタックに追加されます。次に、新しく作成されたスタックの要素が foreach ループを使用して表示されます。次に、contains() メソッドを使用して、要素がスタックに存在するかどうかを確認(rèn)します。次に、スタック內(nèi)の要素の數(shù)が count() メソッドを使用して表示されます。次に、Peek() メソッドを使用して、スタックの最上位の要素が表示されます。次に、Pop() メソッドを使用して、スタックの最上位の要素が削除されます。 Pop() メソッドを使用すると、要素の數(shù)とスタックの要素が再度表示されます。次に、Clear() メソッドを使用してスタックのすべての要素を削除します。 Clear() メソッドを使用すると、要素の數(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)容です。詳細(xì)については、PHP 中國語 Web サイトの他の関連記事を參照してください。

このウェブサイトの聲明
この記事の內(nèi)容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰屬します。このサイトは、それに相當(dāng)する法的責(zé)任を負(fù)いません。盜作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡(luò)ください。

ホットAIツール

Undress AI Tool

Undress AI Tool

脫衣畫像を無料で

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード寫真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

寫真から衣服を削除するオンライン AI ツール。

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中國語版

SublimeText3 中國語版

中國語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

C# の亂數(shù)ジェネレーター C# の亂數(shù)ジェネレーター Sep 03, 2024 pm 03:34 PM

C# の亂數(shù)ジェネレーターのガイド。ここでは、亂數(shù)ジェネレーターの仕組み、擬似亂數(shù)の概念、安全な數(shù)値について説明します。

C# の階乗 C# の階乗 Sep 03, 2024 pm 03:34 PM

C# の Factorial のガイド。ここでは、C# での階乗の概要について、さまざまな例とコード実裝とともに説明します。

マルチスレッドと非同期C#の違い マルチスレッドと非同期C#の違い Apr 03, 2025 pm 02:57 PM

マルチスレッドと非同期の違いは、マルチスレッドが複數(shù)のスレッドを同時に実行し、現(xiàn)在のスレッドをブロックせずに非同期に操作を?qū)g行することです。マルチスレッドは計算集約型タスクに使用されますが、非同期はユーザーインタラクションに使用されます。マルチスレッドの利點は、コンピューティングのパフォーマンスを改善することですが、非同期の利點はUIスレッドをブロックしないことです。マルチスレッドまたは非同期を選択することは、タスクの性質(zhì)に依存します。計算集約型タスクマルチスレッド、外部リソースと相互作用し、UIの応答性を非同期に使用する必要があるタスクを使用します。

C# の素數(shù) C# の素數(shù) Sep 03, 2024 pm 03:35 PM

C# の素數(shù)ガイド。ここでは、C# における素數(shù)の導(dǎo)入と例を、コードの実裝とともに説明します。

C#対C:歴史、進(jìn)化、將來の見通し C#対C:歴史、進(jìn)化、將來の見通し Apr 19, 2025 am 12:07 AM

C#とCの歴史と進(jìn)化はユニークであり、將來の見通しも異なります。 1.Cは、1983年にBjarnestrostrupによって発明され、オブジェクト指向のプログラミングをC言語に導(dǎo)入しました。その進(jìn)化プロセスには、C 11の自動キーワードとラムダ式の導(dǎo)入など、複數(shù)の標(biāo)準(zhǔn)化が含まれます。C20概念とコルーチンの導(dǎo)入、將來のパフォーマンスとシステムレベルのプログラミングに焦點を當(dāng)てます。 2.C#は2000年にMicrosoftによってリリースされました。CとJavaの利點を組み合わせて、その進(jìn)化はシンプルさと生産性に焦點を當(dāng)てています。たとえば、C#2.0はジェネリックを?qū)毪?、C#5.0は非同期プログラミングを?qū)毪筏蓼筏俊¥长欷?、將來の開発者の生産性とクラウドコンピューティングに焦點を當(dāng)てます。

C# のパターン C# のパターン Sep 03, 2024 pm 03:33 PM

C# のパターンのガイド。ここでは、C# のパターンの概要と上位 3 種類について、その例とコード実裝とともに説明します。

XMLの形式を変更する方法 XMLの形式を変更する方法 Apr 03, 2025 am 08:42 AM

XML形式を変更する方法はいくつかあります。Atepadなどのテキストエディターを使用して手動で編集する。 XmlBeautifierなどのオンラインまたはデスクトップXMLフォーマットツールを使用して自動的にフォーマットします。 XSLTなどのXML変換ツールを使用して変換ルールを定義します。または、Pythonなどのプログラミング言語を使用して解析および操作します。元のファイルを変更してバックアップするときは注意してください。

C# の回文 C# の回文 Sep 03, 2024 pm 03:34 PM

C# の回文のガイド。ここでは、C# の回文の概要とロジック、およびそのコードを使用したさまざまなメソッドについて説明します。

See all articles