Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic Overridable Sub Push ( _ obj As Object _ )
Dim instance As Stack Dim obj As Object instance.Push(obj)
public virtual void Push ( Object obj )
public: virtual void Push ( Object^ obj )
public void Push ( Object obj )
public function Push ( obj : Object )
Paramètres
NotesStack est implémenté sous forme de mémoire tampon circulaire.
Si Count équivaut déjà à la capacité, la capacité de Stack est augmentée en réallouant automatiquement le tableau interne ; en outre, les éléments existants sont copiés dans le nouveau tableau avant que le nouvel élément soit ajouté.
référence Null (Nothing en Visual Basic) peut faire l'objet d'un push sur Stack en tant qu'espace réservé, si nécessaire. Il occupe une position de la pile et est traité comme les autres objets.
Si Count est inférieur à la capacité de la pile, Push est une opération O(1). Si la capacité doit être augmentée pour intégrer le nouvel élément, Push devient une opération O(n), où n est égal à Count.
ExempleL'exemple suivant décrit l'ajout d'éléments à Stack, la suppression d'éléments de Stack ou l'affichage de l'élément situé en haut de Stack.
Imports System Imports System.Collections Imports Microsoft.VisualBasic Public Class SamplesStack Public Shared Sub Main() ' Creates and initializes a new Stack. Dim myStack As New Stack() myStack.Push("The") myStack.Push("quick") myStack.Push("brown") myStack.Push("fox") ' Displays the Stack. Console.Write("Stack values:") PrintValues(myStack, ControlChars.Tab) ' Removes an element from the Stack. Console.WriteLine("(Pop)" & ControlChars.Tab & ControlChars.Tab & _ "{0}", myStack.Pop()) ' Displays the Stack. Console.Write("Stack values:") PrintValues(myStack, ControlChars.Tab) ' Removes another element from the Stack. Console.WriteLine("(Pop)" & ControlChars.Tab & ControlChars.Tab & _ "{0}", myStack.Pop()) ' Displays the Stack. Console.Write("Stack values:") PrintValues(myStack, ControlChars.Tab) ' Views the first element in the Stack but does not remove it. Console.WriteLine("(Peek)" & ControlChars.Tab & ControlChars.Tab & _ "{0}", myStack.Peek()) ' Displays the Stack. Console.Write("Stack values:") PrintValues(myStack, ControlChars.Tab) End Sub Public Shared Sub PrintValues(myCollection As IEnumerable, mySeparator As Char) Dim obj As [Object] For Each obj In myCollection Console.Write("{0}{1}", mySeparator, obj) Next obj Console.WriteLine() End Sub 'PrintValues End Class 'SamplesStack ' This code produces the following output. ' ' Stack values: fox brown quick The ' (Pop) fox ' Stack values: brown quick The ' (Pop) brown ' Stack values: quick The ' (Peek) quick ' Stack values: quick The
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push( "The" ); myStack.Push( "quick" ); myStack.Push( "brown" ); myStack.Push( "fox" ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes an element from the Stack. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes another element from the Stack. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Views the first element in the Stack but does not remove it. Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); } public static void PrintValues( IEnumerable myCollection, char mySeparator ) { foreach ( Object obj in myCollection ) Console.Write( "{0}{1}", mySeparator, obj ); Console.WriteLine(); } } /* This code produces the following output. Stack values: fox brown quick The (Pop) fox Stack values: brown quick The (Pop) brown Stack values: quick The (Peek) quick Stack values: quick The */
using namespace System; using namespace System::Collections; void PrintValues( IEnumerable^ myCollection, char mySeparator ); int main() { // Creates and initializes a new Stack. Stack^ myStack = gcnew Stack; myStack->Push( "The" ); myStack->Push( "quick" ); myStack->Push( "brown" ); myStack->Push( "fox" ); // Displays the Stack. Console::Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes an element from the Stack. Console::WriteLine( "(Pop)\t\t{0}", myStack->Pop() ); // Displays the Stack. Console::Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes another element from the Stack. Console::WriteLine( "(Pop)\t\t{0}", myStack->Pop() ); // Displays the Stack. Console::Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Views the first element in the Stack but does not remove it. Console::WriteLine( "(Peek)\t\t{0}", myStack->Peek() ); // Displays the Stack. Console::Write( "Stack values:" ); PrintValues( myStack, '\t' ); } void PrintValues( IEnumerable^ myCollection, char mySeparator ) { IEnumerator^ myEnum = myCollection->GetEnumerator(); while ( myEnum->MoveNext() ) { Object^ obj = safe_cast<Object^>(myEnum->Current); Console::Write( "{0}{1}", mySeparator, obj ); } Console::WriteLine(); } /* This code produces the following output. Stack values: fox brown quick The (Pop) fox Stack values: brown quick The (Pop) brown Stack values: quick The (Peek) quick Stack values: quick The */
import System.*; import System.Collections.*; public class SamplesStack { public static void main(String[] args) { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push("The"); myStack.Push("quick"); myStack.Push("brown"); myStack.Push("fox"); // Displays the Stack. Console.Write("Stack values:"); PrintValues(myStack, '\t'); // Removes an element from the Stack. Console.WriteLine("(Pop)\t\t{0}", myStack.Pop()); // Displays the Stack. Console.Write("Stack values:"); PrintValues(myStack, '\t'); // Removes another element from the Stack. Console.WriteLine("(Pop)\t\t{0}", myStack.Pop()); // Displays the Stack. Console.Write("Stack values:"); PrintValues(myStack, '\t'); // Views the first element in the Stack but does not remove it. Console.WriteLine("(Peek)\t\t{0}", myStack.Peek()); // Displays the Stack. Console.Write("Stack values:"); PrintValues(myStack, '\t'); } //main public static void PrintValues(IEnumerable myCollection, char mySeparator) { IEnumerator l_objmyEnum = myCollection.GetEnumerator(); while (l_objmyEnum.MoveNext()) { Console.Write("{0}{1}", System.Convert.ToString(mySeparator), System.Convert.ToString(l_objmyEnum.get_Current())); } Console.WriteLine(); } //PrintValues } //SamplesStack /* This code produces the following output. Stack values: fox brown quick The (Pop) fox Stack values: brown quick The (Pop) brown Stack values: quick The (Peek) quick Stack values: quick The */
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ