在For Each循环期间从列表中删除项目 [英] Remove items from a list during For Each Loop

查看:91
本文介绍了在For Each循环期间从列表中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在循环中减少List而没有成功。从其他帖子我发现这是不可能的,因为它会导致异常被抛出。我似乎无法为我的情况提供其他解决方案。



由于数据对订单敏感,我无法向后运行。



我认为(从阅读其他解决方案)使用for i = 0可能会起作用而不是For Each XData作为RangePoints,但我不确定该怎么做这是因为我不能想出保留访问相关循环中使用的XData属性的能力。



因为我在第二个循环里面会导致困难吗?



....已经整天苦苦挣扎...这让我发疯,所以任何帮助都会受到赞赏。



I'm trying to reduce a List during looping through it without success. From other posts I realise that this isn't possible as it causes an exception to be thrown. I cant seem to get other solutions offered working for my situation.

I cant run backwards as the data is order sensitive.

I think (from reading other solutions) that using a for i = 0 to might work instead of For Each XData as RangePoints, but I'm unsure how to do this as I can't also figure out to retain the ability to get access to the properties of XData used inside the loop in question.

As I'm inside a second loop will this cause difficulties?

....been struggling with this all day... it's driving me crazy so any help appreciated.

For Each TData As RangePoints In DataList

    For Each XData As RangePoints In RemainingXPoints.Skip(1)
        XTRange = XData.Range - TData.Range
        myAngle = If(XTRange < Thresh, NearAngle, FarAngle)
        Dim Tj = New Double() {TData.Tx, TData.Ty, TData.Tz}
        Dim Bj = New Double() {TData.Bx, TData.By, TData.Bz}
        Dim Xk = New Double() {XData.Tx, XData.Ty, XData.Tz}
        If isLyingInCone(Xk, Tj, Bj, myAngle) = True Then
            ShadowList1.Add(CInt(XData.Tx) & "," & CInt(XData.Ty) & "," & CInt(XData.Tz))
        Else
            'Redefine RemainingXPoints to reduce it somehow?
        End If
    Next XData 'K

Next TData

推荐答案

您应该将要从list1中删除的所有项目收集到另一个列表中,例如名为toBeRemoved。然后,您可以迭代toBeRemoved并从list1中删除每个项目。 toBeRemoved应该是本地的,并且一旦失去范围就会消失。
You should collect all the items you want to remove from list1 into another list e.g called toBeRemoved. Then you can iterate over toBeRemoved and remove each item from list1. toBeRemoved should be local and will vanish as soon as it loses scope.


我会尝试在Stack中存储要删除的项的索引。然后将每个索引从堆栈中弹出并从列表中删除该项。
I'd try storing the indices of the items to be removed in a Stack. Then Pop each index off the Stack and remove that item from the List.


我想这是上一个问题的后续跟踪。

您的代码不是完成,因为我们不知道什么是 RemainingXPoints

我将假设所有点都在 DataList

这是一段代码,用于检查一个点是否为锥形,并为您提供不在锥形中的点列表。

I guess that this is a follow up from previous question.
Your code is not complete since we don't know what is RemainingXPoints.
I will assume that all points are in DataList
Here is a piece of code that check if a point is in cone and gives you the list of points not in cone.
Points= 0
For Scan= 1 to DataList.GetUpperBound(0)
    Visible= True
    For Check= 0 To Points
        XTRange = DataList(Check).Range - DataList(Scan).Range
        myAngle = If(XTRange < Thresh, NearAngle, FarAngle)
        Dim Tj = New Double() {DataList(Scan).Tx, DataList(Scan).Ty, DataList(Scan).Tz}
        Dim Bj = New Double() {DataList(Scan).Bx, DataList(Scan).By, DataList(Scan).Bz}
        Dim Xk = New Double() {DataList(Check).Tx, DataList(Check).Ty, DataList(Check).Tz}
        If isLyingInCone(Xk, Tj, Bj, myAngle) = True Then
            Visible= False
            Exit for
        End If
    Next
    If Visible Then
        Points= Points + 1
        DataList(Points) = DataList(Scan)
    End If
Next scan
' all valid points are between 0 and Points
For Scan= 0 to Points
Next Scan



代码未经测试


The code is untested


这篇关于在For Each循环期间从列表中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆