[英]Remove elements from array. index. C#. List<double>
I need your help. 我需要你的帮助。 It's easy question, I think. 我认为这是一个简单的问题。 So. 所以。 I work in C# with array. 我在C#中使用数组工作。
var TinArr = new List<double>();
Next I have some elements in it. 接下来,我有一些内容。 For example: 例如:
TinArr[0]=0.5;
TinArr[1]=0.6;
TinArr[2]=1.1;
TinArr[3]=1.5;
Ok. 好。 Then I have cycle for remove some of them: 然后我有周期来删除其中一些:
for (var j = 0; j < TinArr.Count; j++)
{
if (TinArr[j] <= 1)
{
TinArr.RemoveAt(j);
}
}
As I understand, when I'll remove element with index "0", next element with index "1" after this action will have index "0". 据我了解,当我删除索引为“ 0”的元素时,此操作之后下一个索引为“ 1”的元素将具有索引“ 0”。 Maybe I'm wrong. 也许我错了。 How can I save index? 如何保存索引? Or maybe I should start cycle from the beginning? 还是我应该从头开始循环? NEED YOU HELP! 需要您的帮助! THANKS! 谢谢!
Do it in reverse rather, something like 相反,相反,像
for (var j =TinArr.Count - 1; j >= 0; j--)
{
if (TinArr[j] <= 1)
{
TinArr.RemoveAt(j);
}
}
You almost did it yourself, only missing post-decrement: 您几乎自己做了,只是缺少了递减:
for (var j = 0; j < TinArr.Count; j++)
if (TinArr[j] <= 1)
TinArr.RemoveAt(j--);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.