2017-03-17 71 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 

我在那裏使用的區分大小寫的比較。

只需向用戶提供可用演員的下拉列表,以便他們不必鍵入它。