本文整理汇总了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