I have defined one list as
List<List<int>> thirdLevelIntersection = new List<List<int>>();
I wrote the code as
for(int i = 0; i < 57; i++)
{
if(my condition)
thirdLevelIntersection[i] = null;
else
{
//some logic
}
}
so i get the list for 0 to 56 values and some arbitrary values are null like thirdlevelIntersection[1],thirdlevelIntersection[10],thirdlevelIntersection[21],thirdlevelIntersection[21],thirdlevelIntersection[14],thirdlevelIntersection[15],thirdlevelIntersection[51](total 7).
Now i want to remove this values from the list.
And have a list from thirdlevelIntersection[0] thirdlevelIntersection[49].
What should I do?
完成循环后,请尝试
thirdLevelIntersection.RemoveAll(list => list == null);
If you're creating thirdLevelIntersection
from a sourceCollection
of some type you can use Linq.
List<List<int>> thirdLevelIntersection =
(from item in sourceCollection
where !(my condition)
select item)
.ToList();
Or if you're building up the list over multiple statements you can do it as you are creating it:
thirdLevelIntersection.AddRange(
from item in sourceCollection
where !(my condition)
select item);
This eliminates the necessity to remove items from a list once they've been added.
You can do this while you're iterating over the list by calling RemoveAt()
and then decrementing i
(so the next value is considered).
List<List<int>> thirdLevelIntersection = new List<List<int>>();
for(int i=0;i<57;i++)
{
if (my condition)
{
thirdLevelIntersection.RemoveAt(i--);
continue;
}
else
{
//some logic
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.