本文整理匯總了VB.NET中System.Collections.Generic.List<T>.BinarySearch方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET List<T>.BinarySearch方法的具體用法?VB.NET List<T>.BinarySearch怎麽用?VB.NET List<T>.BinarySearch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Collections.Generic.List<T>
的用法示例。
在下文中一共展示了List<T>.BinarySearch方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
Dim dinosaurs As New List(Of String)
dinosaurs.Add("Pachycephalosaurus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("Mamenchisaurus")
dinosaurs.Add("Deinonychus")
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & "Sort")
dinosaurs.Sort
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"BinarySearch and Insert ""Coelophysis"":")
Dim index As Integer = dinosaurs.BinarySearch("Coelophysis")
If index < 0 Then
index = index Xor -1
dinosaurs.Insert(index, "Coelophysis")
End If
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"BinarySearch and Insert ""Tyrannosaurus"":")
index = dinosaurs.BinarySearch("Tyrannosaurus")
If index < 0 Then
index = index Xor -1
dinosaurs.Insert(index, "Tyrannosaurus")
End If
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
End Sub
End Class
輸出:
Pachycephalosaurus Amargasaurus Mamenchisaurus Deinonychus Sort Amargasaurus Deinonychus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Coelophysis": Amargasaurus Coelophysis Deinonychus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Tyrannosaurus": Amargasaurus Coelophysis Deinonychus Mamenchisaurus Pachycephalosaurus Tyrannosaurus
示例2: DinoComparer
' 導入命名空間
Imports System.Collections.Generic
Public Class DinoComparer
Implements IComparer(Of String)
Public Function Compare(ByVal x As String, _
ByVal y As String) As Integer _
Implements IComparer(Of String).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
' lengths of the two strings.
'
Dim retval As Integer = _
x.Length.CompareTo(y.Length)
If retval <> 0 Then
' If the strings are not of equal length,
' the longer string is greater.
'
Return retval
Else
' If the strings are of equal length,
' sort them with ordinary string comparison.
'
Return x.CompareTo(y)
End If
End If
End If
End Function
End Class
Public Class Example
Public Shared Sub Main()
Dim dinosaurs As New List(Of String)
dinosaurs.Add("Pachycephalosaurus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("Mamenchisaurus")
dinosaurs.Add("Deinonychus")
Display(dinosaurs)
Dim dc As New DinoComparer
Console.WriteLine(vbLf & "Sort with alternate comparer:")
dinosaurs.Sort(dc)
Display(dinosaurs)
SearchAndInsert(dinosaurs, "Coelophysis", dc)
Display(dinosaurs)
SearchAndInsert(dinosaurs, "Oviraptor", dc)
Display(dinosaurs)
SearchAndInsert(dinosaurs, "Tyrannosaur", dc)
Display(dinosaurs)
SearchAndInsert(dinosaurs, Nothing, dc)
Display(dinosaurs)
End Sub
Private Shared Sub SearchAndInsert( _
ByVal lis As List(Of String), _
ByVal insert As String, ByVal dc As DinoComparer)
Console.WriteLine(vbLf & _
"BinarySearch and Insert ""{0}"":", insert)
Dim index As Integer = lis.BinarySearch(insert, dc)
If index < 0 Then
index = index Xor -1
lis.Insert(index, insert)
End If
End Sub
Private Shared Sub Display(ByVal lis As List(Of String))
Console.WriteLine()
For Each s As String In lis
Console.WriteLine(s)
Next
End Sub
End Class
輸出:
Pachycephalosaurus Amargasaurus Mamenchisaurus Deinonychus Sort with alternate comparer: Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Coelophysis": Coelophysis Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Oviraptor": Oviraptor Coelophysis Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Tyrannosaur": Oviraptor Coelophysis Deinonychus Tyrannosaur Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "": Oviraptor Coelophysis Deinonychus Tyrannosaur Amargasaurus Mamenchisaurus Pachycephalosaurus
示例3: DinoComparer
' 導入命名空間
Imports System.Collections.Generic
Public Class DinoComparer
Implements IComparer(Of String)
Public Function Compare(ByVal x As String, _
ByVal y As String) As Integer _
Implements IComparer(Of String).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
' lengths of the two strings.
'
Dim retval As Integer = _
x.Length.CompareTo(y.Length)
If retval <> 0 Then
' If the strings are not of equal length,
' the longer string is greater.
'
Return retval
Else
' If the strings are of equal length,
' sort them with ordinary string comparison.
'
Return x.CompareTo(y)
End If
End If
End If
End Function
End Class
Public Class Example
Public Shared Sub Main()
Dim dinosaurs As New List(Of String)
dinosaurs.Add("Pachycephalosaurus")
dinosaurs.Add("Parasauralophus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("Galimimus")
dinosaurs.Add("Mamenchisaurus")
dinosaurs.Add("Deinonychus")
dinosaurs.Add("Oviraptor")
dinosaurs.Add("Tyrannosaurus")
Dim herbivores As Integer = 5
Display(dinosaurs)
Dim dc As New DinoComparer
Console.WriteLine(vbLf & _
"Sort a range with the alternate comparer:")
dinosaurs.Sort(0, herbivores, dc)
Display(dinosaurs)
Console.WriteLine(vbLf & _
"BinarySearch a range and Insert ""{0}"":", _
"Brachiosaurus")
Dim index As Integer = _
dinosaurs.BinarySearch(0, herbivores, "Brachiosaurus", dc)
If index < 0 Then
index = index Xor -1
dinosaurs.Insert(index, "Brachiosaurus")
herbivores += 1
End If
Display(dinosaurs)
End Sub
Private Shared Sub Display(ByVal lis As List(Of String))
Console.WriteLine()
For Each s As String In lis
Console.WriteLine(s)
Next
End Sub
End Class
輸出:
Pachycephalosaurus Parasauralophus Amargasaurus Galimimus Mamenchisaurus Deinonychus Oviraptor Tyrannosaurus Sort a range with the alternate comparer: Galimimus Amargasaurus Mamenchisaurus Parasauralophus Pachycephalosaurus Deinonychus Oviraptor Tyrannosaurus BinarySearch a range and Insert "Brachiosaurus": Galimimus Amargasaurus Brachiosaurus Mamenchisaurus Parasauralophus Pachycephalosaurus Deinonychus Oviraptor Tyrannosaurus