【问题标题】:vb.net arraylist dictionary loopvb.net arraylist 字典循环
【发布时间】:2013-09-05 06:33:45
【问题描述】:

我在这里所做的是将两列的表保存在数组列表和字典中。

Dim result As New ArrayList()
While dr.Read()
        ' Insert each column into a dictionary
        Dim dict As New Dictionary(Of String, Object)
        For count As Integer = 0 To (dr.FieldCount - 1)
            dict.Add(dr.GetName(count), dr(count))
        Next
        ' Add the dictionary to the ArrayList
        result.Add(dict)
    End While

………… 现在我想检查它,当它找到时 - 删除它。因为有很多数据,所以可以节省时间。我收到错误“集合已修改;枚举操作可能无法执行。”删除后进入下一步。我理解这个问题,但我该如何克服这个问题?如何将其转换为带删除的循环?

For Each dat As Dictionary(Of String, Object) In result 
                        comp2 = dat("ID") 
                        If comp2 = comp Then
                            advcode = advcode & "," & dat("ADVC")
                            found = True
                            firstattempt = False
                            result.Remove(dat)
                        Else
                            If found And Not firstattempt Then Exit For
                        End If
                    Next

【问题讨论】:

    标签: vb.net for-loop dictionary arraylist


    【解决方案1】:

    为什么不使用List(Of Dictionary(Of String, Object)) 而不是ArrayList?这将使代码更清晰。

    尽管如此,只需将集合复制到一个新集合中,因此从原始集合中删除一个元素不会停止副本的迭代:

    For Each dat As Dictionary(Of String, Object) In result.ToArray() ' Copy into new array '
        comp2 = dat("ID") 
        If comp2 = comp Then
            advcode = advcode & "," & dat("ADVC")
            found = True
            firstattempt = False
            result.Remove(dat)
        Else
            If found And Not firstattempt Then Exit For
        End If
    Next
    

    【讨论】:

      【解决方案2】:

      通常有两种修复方法:

      1. 从后面遍历arraylist:

        For i As Integer = result.Count - 1 To 0 Step -1
            Dim dat = CType(result(i), Dictionary(Of String, Object))
            ' ...
            ' if found:
            result.RemoveAt(i)
        Next
        
      2. 将数组列表复制到一个新数组中:

        For Each Dat As Dictionary(Of String, Object) In result.ToArray()
            ' do your stuff here
        Next
        

      选项 1 应该会提供更好的性能

      【讨论】:

        【解决方案3】:

        试试这个:

        Dim iCont As Integer = 0
        
        While result.Count > iCont
            Dim dat As Dictionary(Of String, Object) = CType(result(iCont), Dictionary(Of String, Object))
            comp2 = dat("ID") 
            If comp2 = comp Then
                advcode = advcode & "," & dat("ADVC")
                found = True
                firstattempt = False
                result.Remove(dat)
            Else
                If found And Not firstattempt Then Exit For
                iCont += 1
            End If   
        End While
        

        【讨论】:

        • 你是对的。我刚刚提出了一个While 循环,但是为了避免这个问题,必须更改代码。已更正。
        猜你喜欢
        • 1970-01-01
        • 2020-02-10
        • 2021-11-25
        • 1970-01-01
        • 1970-01-01
        • 2022-11-17
        • 2012-07-25
        • 2021-05-30
        • 1970-01-01
        相关资源
        最近更新 更多