且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#Arraylist Confusion需要帮助

更新时间:2023-11-24 09:05:10

当你在'addFirst中指定vals [0]时,你覆盖无论其中包含什么价值。



将数组(或ArrayList?)转换为等效的LIFO(上一个,第一个) out)stack,当.NET为你准备好在System.Collections.Generic库中使用时。
When you assign to vals[0] in 'addFirst, you overwrite whatever value is in it.

It's more work than necessary to turn an Array (or ArrayList ?) into the equivalent of LIFO (last in, first out) stack, when .NET gives you one ready to use in the System.Collections.Generic Library.
// create a new integer Stack
private Stack<int> intStack = new Stack<int>();

// somewhere in your code:

// add a value to the Stack
intStack.Push(100);

// return the value at the top of the Stack
int TopInt = intStack.Peek();

// remove and return the value at the top of the Stack
int TopInt = intStack.Pop();

你是什么通过使用堆栈放弃是使用索引来访问不在堆栈顶部的特定元素的任何方式。

What you give up by using a Stack is any way to use an index to access a particular element that is not on "top" of the Stack.