【问题标题】:Binary Searching in a List and Referring to a Specific Column of the List在列表中进行二分查找并引用列表的特定列
【发布时间】:2017-03-17 22:43:09
【问题描述】:

我从文件中读取了一些两列数据并放入列表中,然后按字母顺序排序。

//文件

鹰嘴豆泥,0.75

辣椒,0.50

塔布利,1.25

Tzatziki,0.50

//声明变量和公共属性

    Dim extraList As List(Of extra)

Public Class extra
    Implements IComparable(Of extra)
    Public Property Name As String
    Public Property Price As Decimal
    'Public Property extraList As List(Of extra)

    Public Function CompareTo(other As extra) As Integer Implements IComparable(Of extra).CompareTo
        Return Me.Name.CompareTo(other.Name)
    End Function
End Class

//将数据放入列表并排序

    Sub Get_Extras_List()
    'Reads the extras file and puts the information into a list, splitting the name of the extra and the price into separate columns 
    Dim allExtras = From line In System.IO.File.ReadLines("C:\Users\ExtrasList.txt")
                    Let Columns = line.Split(","c)
                    Where Columns.Length = 2
                    Let Price = Decimal.Parse(Columns(1).Trim())
                    Let Name = Columns(0).Trim()
                    Select New extra With {.Name = Name, .Price = Price}

    extraList = allExtras.ToList()

    'Sort the list alphabetically 
    extraList.Sort()
End Sub

现在我需要编写一个方法,允许用户输入额外的内容并使用二进制搜索来搜索它以查看它是否存在。到目前为止,我已经尝试过这个,但它只是不起作用,即使它做了我如何让它返回一个真值或假值? (存在与否?)

    Sub Search_Extras_List()
    Dim strSearchExtra As String = Console.ReadLine()
    Console.WriteLine(vbLf & "BinarySearch for '{0}':", strSearchExtra)
    Dim index As Integer =
        List(Of extra).BinarySearch(extraList.Name, strSearchExtra)
End Sub

最后,我必须让用户选择其中一项附加服务,然后将其价格添加到总价中。我如何参考价格? extraList.Price?额外的价格?等等。

【问题讨论】:

    标签: vb.net list binary-search


    【解决方案1】:

    如果你想做这样的二分查找,你需要写一个比较器。

    参考List(Of T).BinarySearch Method (T, IComparer(Of T)),可能是

    Public Class ExtraComparer
        Implements IComparer(Of extra)
    
        Public Function Compare(ByVal x As extra, ByVal y As extra) As Integer Implements IComparer(Of extra).Compare
    
            If x Is Nothing Then
                If y Is Nothing Then
                    ' If x is Nothing and y is Nothing, they're equal. 
                    Return 0
                Else
                    ' If x is Nothing and y is not Nothing, y is greater. 
                    Return -1
                End If
            Else
                ' If x is not Nothing...
                If y Is Nothing Then
                    ' ...and y is Nothing, x is greater.
                    Return 1
                Else
                    ' ...and y is not Nothing, compare the names of the two extras.
                    '
                    Return x.Name.CompareTo(y.Name)
    
                End If
            End If
        End Function
    
    End Class
    

    你可以用什么来测试

    ' Get some item from the data so we know it is present...
    Dim a = extraList(2).Name
    Dim lookFor As New extra With {.Name = a}
    Dim idx = extraList.BinarySearch(lookFor, New ExtraComparer)
    Console.WriteLine($"Index of {a} is {idx}.")
    

    (尽管您可能希望进行不区分大小写的字符串比较)。

    如果返回的索引为负数,则未找到该项目。 (有关详细信息,请参阅上面链接的文档。)

    不过,您可能会发现使用 LINQ 更容易:

    Dim a = extraList(2).Name
    Dim chosenExtra = extraList.FirstOrDefault(Function(x) x.Name.Equals(a, StringComparison.CurrentCultureIgnoreCase))
    
    If chosenExtra IsNot Nothing Then
        Console.WriteLine($"User chose {chosenExtra.Name} at a price of {chosenExtra.Price}")
    Else
        Console.WriteLine($"User's choice of ""{a}"" was not found.")
    End If
    

    我在那里使用了不区分大小写的比较。

    或者只需向用户显示可用附加功能的下拉列表,这样他们就不必输入了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2021-12-24
      • 1970-01-01
      • 2023-02-11
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多