Computer >> Máy Tính >  >> Lập trình >> C#

Phương thức Stack.Peek () trong C #

Phương thức Stack.Peek () trong C # được sử dụng để trả về đối tượng ở trên cùng của Ngăn xếp mà không cần xóa nó.

Cú pháp

Cú pháp như sau -

 đối tượng ảo công khai Peek (); 

Ví dụ

Bây giờ chúng ta hãy xem một ví dụ -

 using System; using System.Collections; public class Demo {public static void Main () {Stack stack =new Stack (); stack.Push ("Inspiron"); stack.Push ("Alienware"); stack.Push ("Máy chiếu"); stack.Push ("Màn hình"); stack.Push ("XPS"); stack.Push ("Máy tính xách tay"); stack.Push ("Máy tính xách tay"); Console.WriteLine ("Ngăn xếp các phần tử ..."); foreach (string val trong stack) {Console.WriteLine (val); } Console.WriteLine ("Số phần tử =" + stack.Count); Console.WriteLine ("Phần tử ở đầu =" + stack.Peek ()); stack.Push ("Ultrabook"); stack.Push ("Máy ảnh"); stack.Push ("Bàn phím"); Console.WriteLine ("\ nStack phần tử ... đã cập nhật"); foreach (string val trong stack) {Console.WriteLine (val); } Console.WriteLine ("Phần tử ở đầu =" + stack.Peek ()); Console.WriteLine ("\ nTính số phần tử (đã cập nhật) =" + stack.Count); stack.Clear (); Console.Write ("Số phần tử (đã cập nhật) =" + stack.Count); }} 

Đầu ra

Điều này sẽ tạo ra kết quả sau -

 Stack element ... NotebookLaptopXPSMonitorsProjectorsAlienwareInspironCount of Elements =7Element at the top =NotebookStack Elements ... updatedKeyboardsCamerasUltrabookNotebookLaptopXPSMonitorsProjectorsAlienwareInspironElement at the top =KeyboardsCount of Elements (updated) =10 

Ví dụ

Bây giờ chúng ta hãy xem một ví dụ khác -

 using System; using System.Collections; public class Demo {public static void Main () {Stack stack =new Stack (); stack.Push ("Inspiron"); stack.Push ("Alienware"); stack.Push ("Máy chiếu"); stack.Push ("Màn hình"); stack.Push ("XPS"); stack.Push ("Máy tính xách tay"); stack.Push ("Máy tính xách tay"); Console.WriteLine ("Ngăn xếp các phần tử ..."); foreach (string val trong stack) {Console.WriteLine (val); } Console.WriteLine ("Số phần tử =" + stack.Count); Console.WriteLine ("Phần tử Loa là ngăn xếp? =" + Stack.Contains ("Loa")); stack.Push ("Tai nghe"); stack.Push ("Bàn phím"); stack.Push ("Tai nghe"); Console.WriteLine ("\ nStack phần tử ... đã cập nhật"); foreach (string val trong stack) {Console.WriteLine (val); } Console.WriteLine ("Số phần tử (đã cập nhật) =" + stack.Count); Console.WriteLine ("Phần tử Alienware là ngăn xếp? =" + Stack.Contains ("Alienware")); Stack stack2 =(Stack) stack.Clone (); Console.WriteLine ("\ nStack các phần tử ... được nhân bản"); foreach (string val trong stack2) {Console.WriteLine (val); } Console.Write ("Số phần tử (đã cập nhật) =" + stack2.Count); Console.WriteLine ("Top of the Stack =" + stack2.Peek ()); }} 

Đầu ra

Điều này sẽ tạo ra kết quả sau -

 Stack element ... NotebookLaptopXPSMonitorsProjectorsAlienwareInspironCount of Elements =7Element at the top =NotebookStack Elements ... updatedKeyboardsCamerasUltrabookNotebookLaptopXPSMonitorsProjectorsAlienwareInspironElement at the top =KeyboardsCount of Elements (updated) =10