You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中List缺失数据填充的循环优化方案求助

优化方案:规避异常捕获的List补全实现

问题场景

需将字节数组转换后的UInt32数据存入Sizes、Addresses、Indexes、NumberOfItems、Description五个List,原始数据为压缩存储,需完成两步补全:

  • 步骤1:补全Indexes为连续值(如缺失2、3、4则插入对应值,其余列补1UI),最终所有列长度与Sizes一致;
  • 步骤2:当Number_of_items列值为2时,将对应行下方的相关列内容下移一行补全空位。

当前实现通过While True结合捕获ArgumentOutOfRangeException控制循环,因List长度动态变化导致循环次数不足,且异常捕获作为控制流不符合最佳实践,以下是更可靠的实现方案。


优化后的代码实现

改进的Reprocessing_of_parsed_data类

Public NotInheritable Class Reprocessing_of_parsed_data
    Public Property Sizes As New List(Of UInteger)
    Public Property Addresses As New List(Of UInteger)
    Public Property Indexes As New List(Of UInteger)
    Public Property Number_of_items As New List(Of UInteger)
    Public Property Description As New List(Of UInteger)

    Public Sub New(sizes As List(Of UInt32), addresses As List(Of UInt32), indexes As List(Of UInt32), number_of_items As List(Of UInt32), description As List(Of UInt32))
        Me.Sizes = sizes
        Me.Addresses = addresses
        Me.Indexes = indexes
        Me.Number_of_items = number_of_items
        Me.Description = description
    End Sub

    Public Sub Fill_gaps()
        ' 步骤1:补全Indexes为连续值,同步补全其他列
        CompleteIndexesAndRelatedColumns()
        
        ' 步骤2:处理Number_of_items为2的情况,下移内容补空位
        ProcessNumberOfItems()
    End Sub

    Private Sub CompleteIndexesAndRelatedColumns()
        Dim targetCount = Sizes.Count
        Dim currentIndex As UInt32 = 1UI
        Dim currentListIndex As Integer = 0

        Do While Indexes.Count < targetCount
            If currentListIndex >= Indexes.Count OrElse Indexes(currentListIndex) > currentIndex Then
                ' 插入缺失的Index,其他列补1UI
                Indexes.Insert(currentListIndex, currentIndex)
                Number_of_items.Insert(currentListIndex, 1UI)
                Description.Insert(currentListIndex, 1UI)
                ' 补全Addresses列
                If currentListIndex < Addresses.Count Then
                    Addresses.Insert(currentListIndex, 1UI)
                Else
                    Addresses.Add(1UI)
                End If
                currentIndex += 1UI
            ElseIf Indexes(currentListIndex) = currentIndex Then
                ' 当前Index匹配,移动到下一个
                currentIndex += 1UI
                currentListIndex += 1
            Else
                ' 跳过重复Index(原始数据无重复可忽略此分支)
                currentListIndex += 1
            End If
        Loop

        ' 确保Addresses长度与Sizes一致
        While Addresses.Count < targetCount
            Addresses.Add(1UI)
        End While
    End Sub

    Private Sub ProcessNumberOfItems()
        ' 反向遍历避免插入元素导致的索引偏移
        Dim i As Integer = Number_of_items.Count - 1
        Do While i >= 0
            If Number_of_items(i) = 2UI Then
                ' 在当前行下方插入空位,下移后续内容
                If i + 1 <= Addresses.Count Then
                    Addresses.Insert(i + 1, UInteger.MaxValue)
                    Indexes.Insert(i + 1, UInteger.MaxValue)
                    Number_of_items.Insert(i + 1, UInteger.MaxValue)
                    Description.Insert(i + 1, UInteger.MaxValue)
                    ' 同步补全Sizes占位(可根据需求修改占位值)
                    If i + 1 <= Sizes.Count Then
                        Sizes.Insert(i + 1, 0UI)
                    End If
                End If
            End If
            i -= 1
        Loop
    End Sub
End Class

优化说明

  1. 抛弃异常捕获控制流:通过明确的循环条件(如Indexes.Count < targetCount、i >= 0)控制循环,逻辑清晰且性能更优;
  2. 反向遍历处理插入:步骤2采用反向遍历,避免插入元素后后续索引偏移导致的遗漏;
  3. 明确的补全逻辑:步骤1直接以Sizes.Count为目标长度,逐行检查并插入缺失的Index,确保所有列最终长度一致;
  4. 逻辑拆分:将两步补全拆分为独立子方法,提升代码可读性与可维护性。

测试代码(原FormMain.vb可直接使用)

Public NotInheritable Class FormMain
    Private Sizes As New List(Of UInt32) From {
        58_355UI,
        20_270UI,
        4_830UI,
        4_443UI,
        25_177UI,
        8_844UI,
        4_101UI,
        4_200UI,
        14_991UI,
        12_639UI,
        12_894UI,
        14_165UI,
        12_954UI,
        26_670UI,
        7_388UI}

    Private Addresses As New List(Of UInt32) From {4_323UI, 62_706UI, 83_646UI, 88_935UI, 93_883UI, 128_259UI, 132_718UI,
        137_254UI, 152_590UI, 178_485UI, 193_022UI, 206_718UI}

    Private Indexes As New List(Of UInt32) From {1UI, 5UI, 6UI, 9UI, 10UI, 12UI}

    Private NumberOfItems As New List(Of UInt32) From {1UI, 2UI, 1UI, 2UI, 1UI, 2UI}
    Private Description As New List(Of UInt32) From {1UI, 1UI, 1UI, 1UI, 1UI, 1UI}
    
    Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
        Dim RopD As New Reprocessing_of_parsed_data(Sizes, Addresses, Indexes, NumberOfItems, Description)
        RopD.Fill_gaps()
    End Sub
End Class

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:11:17