您可以使用LINQ实现它:
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
int top = stack.ElementAt(0); // Returns 4
int next = stack.ElementAt(1); // Returns 3
Run Code Online (Sandbox Code Playgroud)
但是,如果您发现自己试图通过索引访问堆栈中的元素,那么您肯定做错了,您应该重新设计解决方案.
| 归档时间: |
|
| 查看次数: |
3963 次 |
| 最近记录: |