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

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

C# スタック

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

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

構(gòu)文:

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

Stack stack_name = new Stack();

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

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

  • 後入れ先出しの順序でスタックの要素にアクセスする必要がある場(chǎng)合は、Stack と呼ばれるオブジェクトのコレクションを使用します。
  • スタックに要素を追加するプロセスは、要素をスタックにプッシュすると呼ばれ、スタックから要素を削除するプロセスは、スタックから要素をポップすると呼ばれます。
  • スタックに要素を追加するとスタックのサイズが増加するため、スタックは要素の動(dòng)的なコレクションです。
  • スタックが保持できる要素の數(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 中國(guó)語(yǔ) Web サイトの他の関連記事を參照してください。

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

ホットAIツール

Undress AI Tool

Undress AI Tool

脫衣畫(huà)像を無(wú)料で

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

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

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無(wú)料のコードエディター

SublimeText3 中國(guó)語(yǔ)版

SublimeText3 中國(guó)語(yǔ)版

中國(guó)語(yǔ)版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

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

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開(kāi)発ツール

SublimeText3 Mac版

SublimeText3 Mac版

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

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

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

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

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

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

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

C# の素?cái)?shù) C# の素?cái)?shù) Sep 03, 2024 pm 03:35 PM

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

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

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

C#マルチスレッドプログラミングとは何ですか? C#マルチスレッドプログラミングでは、C#マルチスレッドプログラミングを使用します C#マルチスレッドプログラミングとは何ですか? C#マルチスレッドプログラミングでは、C#マルチスレッドプログラミングを使用します Apr 03, 2025 pm 02:45 PM

C#マルチスレッドプログラミングは、プログラムが複數(shù)のタスクを同時(shí)に実行できるようにするテクノロジーです。パフォーマンスを改善し、応答性を改善し、並列処理を?qū)g裝することにより、プログラムの効率を改善できます。スレッドクラスはスレッドを直接作成する方法を提供しますが、タスクやAsync/待望などの高度なツールは、より安全な非同期操作とクリーンなコード構(gòu)造を提供できます。マルチスレッドプログラミングの一般的な課題には、デッドロック、レース條件、リソースリークが含まれます。これらのリソースモデルの設(shè)計(jì)と、これらの問(wèn)題を回避するために適切な同期メカニズムの使用が必要です。

XMLをJSONに変換する方法 XMLをJSONに変換する方法 Apr 03, 2025 am 09:09 AM

XMLをJSONに変換する方法は次のとおりです。プログラミング言語(yǔ)(Python、Java、C#など)でスクリプトまたはプログラムを作成して変換します。オンラインツール(XMLからJSON、GojkoのXMLコンバーター、XMLオンラインツールなど)を使用してXMLデータを貼り付けまたはアップロードし、JSON形式の出力を選択します。 XMLからJSONコンバーターを使用して変換タスクを?qū)g行します(酸素XMLエディター、Stylus Studio、Altova XMLSpyなど)。 XSLT StyleSheetsを使用してXMLをJSONに変換します。データ統(tǒng)合ツールを使用しています(Informaticなど

XMLを単語(yǔ)に変換する方法 XMLを単語(yǔ)に変換する方法 Apr 03, 2025 am 08:15 AM

XMLを単語(yǔ)に変換するには、Microsoft Wordの使用、XMLコンバーターの使用、またはプログラミング言語(yǔ)の使用方法が3つあります。

See all articles