在 C# 中,我试图从随机索引的列表中获取一个项目。检索到它后,我希望将其删除,以便不再选择它。似乎我需要很多操作才能做到这一点,难道没有一个函数可以让我简单地从列表中提取一个项目吗?RemoveAt(index) 函数无效。我想要一个有返回值的。
我在做什么:
List<int> numLst = new List<int>();
numLst.Add(1);
numLst.Add(2);
do
{
int index = rand.Next(numLst.Count);
int extracted = numLst[index];
// do something with extracted value...
numLst.removeAt(index);
}
while(numLst.Count > 0);
我想做的事:
List<int> numLst = new List<int>();
numLst.Add(1);
numLst.Add(2);
do
{
int extracted = numLst.removeAndGetItem(rand.Next(numLst.Count));
// do something with this value...
}
while(numLst.Count > 0);
是否存在这样的“removeAndGetItem”功能?