2017-03-17 5 views
0

ファイルから読み込んだリストの中にアルファベット順に並べ替えた2つのカラムデータがあります。リストのバイナリ検索とリストの特定のカラムの参照

//ファイル

フムス、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 

それをソートします。これまで私はこれを試しましたが、それはうまくいかず、たとえそれが真実か偽りの価値を返すにはどうすればよいでしょうか? (それが存在するかどう?)

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?余計です。

答えて

0

このようなバイナリ検索を実行する場合は、比較機能を作成する必要があります。 List(Of T).BinarySearch Method (T, IComparer(Of T))を参照

、それはあなたが

' 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}.") 

(あなたはおそらく、大文字と小文字を区別しない文字列比較をしたいだろうが)でテストすることができます

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 

である可能性があります。

返されるインデックスが負の場合、そのアイテムは見つかりませんでした。 (詳細は、上記にリンクされているドキュメントを参照してください)

ただし、あなたはそれが簡単に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 

私はそこで、大文字と小文字を区別しない比較を使用しました。

または利用可能なエクストラのドロップダウンを表示するだけで、入力する必要がなくなります。

関連する問題