【发布时间】:2016-07-18 18:46:17
【问题描述】:
我有两个列表。 第一个列表包含字母和数字等值。 长度为 [0]-[36]。 第二个列表包含相似的值,长度也是 [0]-[36]。
我使用特定值迭代第二个列表两次以获取索引键,当我从第二个列表中获取索引键时,我想根据第二个列表中的索引删除第一个列表中的项目。
问题是第二次迭代不再起作用,因为第一个列表中的索引键已更改。
我可能应该将列表转换为数组(数组具有固定的索引键,列表在之后生成)但我不知道如何从数组中添加或删除索引键。
我不使用 Linq。
感谢您的帮助和建议 BR
代码示例:
List<int> list_z = new List<int>();
List<int> list_k = new List<int>();
for (int i = 0; i < second_list.Count; i++) {
if (second_list[i] == "K")
{
list_k.Add(i);
}
}
int k = list_k.Count;
for (int i = 0; i < k; i++) {
first_list.RemoveAt(list_k[i]);
}
for (int i = 0; i < second_list.Count; i++)
{
if (second_list[i] == "Z")
{
list_z.Add(i);
}
}
int z = list_z.Count;
for (int i = 0; i < svi_z; i++)
first_list.RemoveAt(lista_z[i]); //here is error, because first_list doesnt have index key number 36 anymore
}
【问题讨论】:
-
你会用字典吗?你在这里的目标是什么?我认为可能有一个更简单的解决方案。