stack示例
C#Stack.Contains()方法 (C# Stack.Contains() method)
Stack.Contains() method is used to check whether an element/object exists in the stack or not, it returns true if an object/element exists in the stack else it returns false.
Stack.Contains()方法用于检查堆栈中是否存在元素/对象,如果堆栈中存在对象/元素,则返回true ,否则返回false 。
Syntax:
句法:
bool Stack.Clone(Object);
Parameters: Object – to be checked, whether it exists in the stack or not.
参数: Object-要检查的对象 ,是否存在于堆栈中。
Return value: bool – returns true if object exists in the stack else returns false.
返回值: 布尔 -如果在堆栈否则返回false存在对象返回true。
Example:
例:
declare and initialize a stack:
Stack stk = new Stack();
insertting elements:
stk.Push(100);
stk.Push(200);
stk.Push(300);
stk.Push(400);
stk.Push(500);
checking elements:
stk.Contains(100);
stk.Contains(800);
Output:
true
false
C#示例使用Stack.Contains()方法检查堆栈中是否存在对象/元素 (C# example to check whether an object/element exists in the stack or not using Stack.Contains() method)
using System;
using System.Text;
using System.Collections;
namespace Test
{
class Program
{
//function to print stack elements
static void printStack(Stack s)
{
foreach (Object obj in s)
{
Console.Write(obj + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//declare and initialize a stack
Stack stk = new Stack();
//insertting elements
stk.Push(100);
stk.Push(200);
stk.Push(300);
stk.Push(400);
stk.Push(500);
//printing stack elements
Console.WriteLine("Stack elements are...");
printStack(stk);
//checking elements
if (stk.Contains(100) == true)
Console.WriteLine("100 exists in the stack");
else
Console.WriteLine("100 does not exist in the stack");
if (stk.Contains(800) == true)
Console.WriteLine("800 exists in the stack");
else
Console.WriteLine("800 does not exist in the stack");
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
输出量
Stack elements are...
500 400 300 200 100
100 exists in the stack
800 does not exist in the stack
Reference: Stack.Contains(Object) Method
翻译自: https://www.includehelp.com/dot-net/stack-contains-method-with-example-in-c-sharp.aspx
stack示例