2017-03-17 67 views
0

我有一些两列数据,我从文件中读取并放入列表中,然后按字母顺序排序。在列表中的二进制搜索和引用列表的特定列

//文件

鹰嘴豆泥,0.75

辣椒,0.50

Tabouli,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 

现在我需要编写一个方法,它允许用户键入一个额外的搜索使用二进制搜索,看是否存在它。到目前为止,我已经尝试过,但它不起作用,即使它如何让它返回true或false值? (如果它的存在与否?)

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? extra.Price?等等。

回答

0

如果你想做这样的二进制搜索,你将需要编写一个比较器。

参考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 

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

只需向用户提供可用演员的下拉列表,以便他们不必键入它。