Stack.Peek 方法

返回位于 Stack 顶部的对象但不将其移除。

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable Function Peek As Object
用法
Dim instance As Stack
Dim returnValue As Object

returnValue = instance.Peek
public virtual Object Peek ()
public:
virtual Object^ Peek ()
public Object Peek ()
public function Peek () : Object

返回值

位于 Stack 顶部的 Object

异常

异常类型 条件

InvalidOperationException

Stack 为空。

备注

此方法类似于 Pop 方法,但 Pop 不修改 Stack

如果需要,可以将 空引用(在 Visual Basic 中为 Nothing) 推入到 Stack 上作为占位符。若要区分空值和堆栈结尾,请检查 Count 属性或捕捉 Stack 为空时引发的 InvalidOperationException

此方法的运算复杂度为 O(1)。

示例

下面的示例演示如何向 Stack 中添加元素、如何从 Stack 中移除元素以及如何查看 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
 */

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Stack 类
Stack 成员
System.Collections 命名空间
Pop
Push