对于遇到的每个循环'集合已修改;枚举操作可能无法执行,但我需要在运行时更改列表的大小

发布于 2025-01-10 00:00:24 字数 1684 浏览 1 评论 0原文

我正在尝试使用 .NET 6 在 VB 中编写霍夫曼编码系统。这是当前的主要代码,使用“ConstructionQueue”作为优先级队列来处理添加到“Tree”列表中的节点。 'Node' 是一个抽象类,'LeafNode' 和 'InternalNode' 继承自该抽象类;当这个类运行时,ConstructionQueue 仅填充了 LeafNode。我的问题是,我需要在初始化过程中将 InternalNode 添加到队列中进行处理。这是当前的代码:

Public Class HuffmanTree

    Private Tree As New List(Of Node)
    
     Public Sub New(ByVal ConstructionQueue As PriorityQueue)
        Dim PairMade As Boolean = False
        Dim NodesProcessed As Integer = 0
        Dim TempWeight As Integer
        For Each Node In ConstructionQueue.QueueFrame

            If ConstructionQueue.GetFrontPointer <> 0 And ConstructionQueue.GetFrontPointer Mod 2 = 0 Then
                PairMade = True
            End If
            Tree.Insert(NodesProcessed, Node)

            If PairMade = True Then
                
                Dim TempNode As New InternalNode(ConstructionQueue.QueueFrame(ConstructionQueue.GetFrontPointer).GetWeight + TempWeight)

                TempNode.SetLeftPointer(Tree(NodesProcessed - 1))
                TempNode.SetRightPointer(Tree(NodesProcessed))    
                ConstructionQueue.Enqueue(TempNode)      
                PairMade = False
            Else
                TempWeight = Node.GetWeight    
            End If
           
            NodesProcessed += 1
            ConstructionQueue.SetFrontPointer(NodesProcessed)

        Next

    End Sub

End Class

集合已修改;枚举操作可能无法执行。

是我在 End Sub 之前的“Next”中收到的错误。我完全理解为什么,问题是我无法更改“QueueFrame”列表的大小,但我需要一种方法来执行此操作,该方法允许我遍历列表中的每个节点,同时能够更改它的大小,因为在使用它时,我需要将多个实例添加到列表中。

有谁知道有任何解决方法或修复此问题吗?我很乐意提供所需的更多信息。

I'm attempting to code a Huffman Encoding system in VB, using .NET 6. Here is the main code as it currently stands, using 'ConstructionQueue' as a priority queue to handle the nodes being added to the 'Tree' list. 'Node' is an abstract class from which 'LeafNode' and 'InternalNode' inherit; at the time this class is run, ConstructionQueue is filled with only LeafNode's. My issue is that I need to add InternalNodes to the queue during this initialization process to be handled. Here is the current code:

Public Class HuffmanTree

    Private Tree As New List(Of Node)
    
     Public Sub New(ByVal ConstructionQueue As PriorityQueue)
        Dim PairMade As Boolean = False
        Dim NodesProcessed As Integer = 0
        Dim TempWeight As Integer
        For Each Node In ConstructionQueue.QueueFrame

            If ConstructionQueue.GetFrontPointer <> 0 And ConstructionQueue.GetFrontPointer Mod 2 = 0 Then
                PairMade = True
            End If
            Tree.Insert(NodesProcessed, Node)

            If PairMade = True Then
                
                Dim TempNode As New InternalNode(ConstructionQueue.QueueFrame(ConstructionQueue.GetFrontPointer).GetWeight + TempWeight)

                TempNode.SetLeftPointer(Tree(NodesProcessed - 1))
                TempNode.SetRightPointer(Tree(NodesProcessed))    
                ConstructionQueue.Enqueue(TempNode)      
                PairMade = False
            Else
                TempWeight = Node.GetWeight    
            End If
           
            NodesProcessed += 1
            ConstructionQueue.SetFrontPointer(NodesProcessed)

        Next

    End Sub

End Class

Collection was modified; enumeration operation may not execute.

is the error I receive on the 'Next' before End Sub. I completely understand why, the issue is that I can't alter the size of the 'QueueFrame' List, but I need a method of doing this that is going to allow me to go through each node in the list whilst being able to change its size, as there will be multiple instances where I need to add to the list as I'm using it.

Does anyone know of any sort of workaround or fix to this? I'd be happy to provide any more information needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文