C# Stack.Clear() 方法
Stack.Peek() 方法用於從堆棧中移除所有對象。
用法:
void Stack.Clear();
參數:空
返回值: void
——它什麽都不返回。
例:
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);
clearing all objects/elements:
stk.Clear();
Output:
None
使用 Stack.Clear() 方法從堆棧中清除所有對象的 C# 示例
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);
Console.WriteLine("Total number of objects in the stack:" + stk.Count);
//printing stack elements
Console.WriteLine("Stack elements are...");
printStack(stk);
//clearing all objects/elements
stk.Clear();
Console.WriteLine("Total number of objects in the stack:" + stk.Count);
//hit ENTER to exit
Console.ReadLine();
}
}
}
輸出
Total number of objects in the stack:5 Stack elements are... 500 400 300 200 100 Total number of objects in the stack:0
參考:Stack.Clear 方法
相關用法
- C# Stack.Clear用法及代碼示例
- C# Stack.Clone()用法及代碼示例
- C# Stack.Contains()用法及代碼示例
- C# Stack.CopyTo()用法及代碼示例
- C# Stack.Count用法及代碼示例
- C# Stack.Equals()用法及代碼示例
- C# Stack.IsSynchronized用法及代碼示例
- C# Stack.ToArray()用法及代碼示例
- C# Stack.Push()用法及代碼示例
- C# Stack.Peek()用法及代碼示例
- C# Stack.Peek用法及代碼示例
- C# Stack.Synchronized()用法及代碼示例
- C# Stack.ToString()用法及代碼示例
- C# Stack.Pop()用法及代碼示例
- C# Stack.GetEnumerator用法及代碼示例
- C# String.ToUpperInvariant用法及代碼示例
- C# StringBuilder.ToString用法及代碼示例
- C# String ToLower()用法及代碼示例
- C# String ToString()用法及代碼示例
- C# String Contains()用法及代碼示例
注:本文由純淨天空篩選整理自 Stack.Clear() method with example in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。