整数变化数列表的集合以访问不同的变量名称

Collection of list of integer changing number to access the different variable names

Public p1 = New List(Of Integer)({1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
Public p2 = New List(Of Integer)({5, 4, 6, 7, 3, 8, 9, 10, 2, 11})
Public p3 = New List(Of Integer)({11, 8, 10, 9, 7, 12, 6, 13, 14, 15})

我想根据A的多少来显示列表p。如果a是1那么它就是p1,如果a是2那么它就是p2。我如何使用此循环访问列表,而不必多次手动编写代码。

   For a As Integer = 1 To 20
                If strDigit(str) = a Then
                    If a = 1 Then
                        Dim astr As Integer = 0
                        For Each num In p1
                            astr = Val(astr) + 1
                            If num + 1 >= 1 AndAlso num + 1 <= 1000 Then
                                If pos < 5 Then

                                Else

                                End If
                            End If
                        Next
                    ElseIf a = 2 Then
                        For Each num In p2
                            Dim astr As Integer = 0
                            If num + 1 >= 1 AndAlso num + 1 <= 1000 Then
                                If pos < 5 Then

                                Else

                                End If
                            End If
                        Next
                    ElseIf a = 3 Then
                        For Each num In p3
                            Dim astr As Integer = 0
                            astr = Val(astr) + 1
                            If num + 1 >= 1 AndAlso num + 1 <= 1000 Then
                                If pos < 5 Then

                                Else

                            End If
                        Next

通常情况下应该是这样,但事实并非如此。

   For a As Integer = 1 To 20
                If strDigit(str) = a Then
                    If a = 1 Then ' remove line
                        Dim astr As Integer = 0
                        For Each num In p(a) ' here
                            astr = Val(astr) + 1
                            If num + 1 >= 1 AndAlso num + 1 <= 1000 Then
                                If pos < 5 Then

                                Else

                                End If
                            End If
                        Next
                      End If

LINQ 是使通常涉及循环的代码更加简洁的好方法。

Dim lists = {p1, p2, p3}
Dim list = lists.First(Function(l) l.Contains(A))

如果有多个列表包含指定的数字,则该代码将 return 第一个找到它的列表,因此这取决于您搜索它们的顺序。

用排序列表试试这个

  Dim slist As New SortedList(Of Integer, List(Of Integer))

            Dim p1 = New List(Of Integer)({1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
            slist.Add(1, p1)

            Dim p2 = New List(Of Integer)({5, 4, 6, 7, 3, 8, 9, 10, 2, 11})
            slist.Add(2, p2)
            Dim p3 = New List(Of Integer)({11, 8, 10, 9, 7, 12, 6, 13, 14, 15})
            slist.Add(3, p3)
            Dim p4 = New List(Of Integer)({9, 16, 2, 43, 12, 11, 21, 22, 23})
            slist.Add(4, p4)

            Dim tmpList As List(Of Integer)

            For Each a In slist.Keys
                If slist.ContainsKey(a) = True Then
                    tmpList = slist.Item(a)

                    For Each num In tmpList
                        astr = Val(astr) + 1
                        If num + 1 >= 1 AndAlso num + 1 <= 1000 Then
                            If pos < 5 Then

                            Else

                            End If
                        End If

                    Next

                End If


            Next