本文整理匯總了VB.NET中System.Array.BinarySearch方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Array.BinarySearch方法的具體用法?VB.NET Array.BinarySearch怎麽用?VB.NET Array.BinarySearch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Array
的用法示例。
在下文中一共展示了Array.BinarySearch方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: SamplesArray
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array.
Dim myIntArray As Array = Array.CreateInstance( GetType(Int32), 5 )
myIntArray.SetValue( 8, 0 )
myIntArray.SetValue( 2, 1 )
myIntArray.SetValue( 6, 2 )
myIntArray.SetValue( 3, 3 )
myIntArray.SetValue( 7, 4 )
' Do the required sort first
Array.Sort(myIntArray)
' Displays the values of the Array.
Console.WriteLine("The Int32 array contains the following:")
PrintValues(myIntArray)
' Locates a specific object that does not exist in the Array.
Dim myObjectOdd As Object = 1
FindMyObject(myIntArray, myObjectOdd)
' Locates an object that exists in the Array.
Dim myObjectEven As Object = 6
FindMyObject(myIntArray, myObjectEven)
End Sub
Public Shared Sub FindMyObject(myArr As Array, myObject As Object)
Dim myIndex As Integer = Array.BinarySearch(myArr, myObject)
If myIndex < 0 Then
Console.WriteLine("The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, Not(myIndex))
Else
Console.WriteLine("The object to search for ({0}) is at index {1}.", myObject, myIndex)
End If
End Sub
Public Shared Sub PrintValues(myArr As Array)
Dim i As Integer = 0
Dim cols As Integer = myArr.GetLength( myArr.Rank - 1 )
For Each o As Object In myArr
If i < cols Then
i += 1
Else
Console.WriteLine()
i = 1
End If
Console.Write( vbTab + "{0}", o)
Next o
Console.WriteLine()
End Sub
End Class
輸出:
The Int32 array contains the following: 2 3 6 7 8 The object to search for (1) is not found. The next larger object is at index 0 The object to search for (6) is at index 2.
示例2: Example
' 導入命名空間
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { _
"Pachycephalosaurus", _
"Amargasaurus", _
"Tyrannosaurus", _
"Mamenchisaurus", _
"Deinonychus", _
"Edmontosaurus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & "Sort")
Array.Sort(dinosaurs)
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"BinarySearch for 'Coelophysis':")
Dim index As Integer = _
Array.BinarySearch(dinosaurs, "Coelophysis")
ShowWhere(dinosaurs, index)
Console.WriteLine(vbLf & _
"BinarySearch for 'Tyrannosaurus':")
index = Array.BinarySearch(dinosaurs, "Tyrannosaurus")
ShowWhere(dinosaurs, index)
End Sub
Private Shared Sub ShowWhere(Of T) _
(ByVal array() As T, ByVal index As Integer)
If index < 0 Then
' If the index is negative, it represents the bitwise
' complement of the next larger element in the array.
'
index = index Xor -1
Console.Write("Not found. Sorts between: ")
If index = 0 Then
Console.Write("beginning of array and ")
Else
Console.Write("{0} and ", array(index - 1))
End If
If index = array.Length Then
Console.WriteLine("end of array.")
Else
Console.WriteLine("{0}.", array(index))
End If
Else
Console.WriteLine("Found at index {0}.", index)
End If
End Sub
End Class
輸出:
Pachycephalosaurus Amargasaurus Tyrannosaurus Mamenchisaurus Deinonychus Edmontosaurus Sort Amargasaurus Deinonychus Edmontosaurus Mamenchisaurus Pachycephalosaurus Tyrannosaurus BinarySearch for 'Coelophysis': Not found. Sorts between: Amargasaurus and Deinonychus. BinarySearch for 'Tyrannosaurus': Found at index 5.
示例3: ReverseComparer
' 導入命名空間
Imports System.Collections.Generic
Public Class ReverseComparer
Implements IComparer(Of String)
Public Function Compare(ByVal x As String, _
ByVal y As String) As Integer _
Implements IComparer(Of String).Compare
' Compare y and x in reverse order.
Return y.CompareTo(x)
End Function
End Class
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { _
"Pachycephalosaurus", _
"Amargasaurus", _
"Tyrannosaurus", _
"Mamenchisaurus", _
"Deinonychus", _
"Edmontosaurus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Dim rc As New ReverseComparer()
Console.WriteLine(vbLf & "Sort")
Array.Sort(dinosaurs, rc)
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"BinarySearch for 'Coelophysis':")
Dim index As Integer = _
Array.BinarySearch(dinosaurs, "Coelophysis", rc)
ShowWhere(dinosaurs, index)
Console.WriteLine(vbLf & _
"BinarySearch for 'Tyrannosaurus':")
index = Array.BinarySearch(dinosaurs, "Tyrannosaurus", rc)
ShowWhere(dinosaurs, index)
End Sub
Private Shared Sub ShowWhere(Of T) _
(ByVal array() As T, ByVal index As Integer)
If index < 0 Then
' If the index is negative, it represents the bitwise
' complement of the next larger element in the array.
'
index = index Xor -1
Console.Write("Not found. Sorts between: ")
If index = 0 Then
Console.Write("beginning of array and ")
Else
Console.Write("{0} and ", array(index - 1))
End If
If index = array.Length Then
Console.WriteLine("end of array.")
Else
Console.WriteLine("{0}.", array(index))
End If
Else
Console.WriteLine("Found at index {0}.", index)
End If
End Sub
End Class
輸出:
Pachycephalosaurus Amargasaurus Tyrannosaurus Mamenchisaurus Deinonychus Edmontosaurus Sort Tyrannosaurus Pachycephalosaurus Mamenchisaurus Edmontosaurus Deinonychus Amargasaurus BinarySearch for 'Coelophysis': Not found. Sorts between: Deinonychus and Amargasaurus. BinarySearch for 'Tyrannosaurus': Found at index 0.
示例4: MainClass
' 導入命名空間
Imports System
Imports System.Collections
Public Class MainClass
Shared Private integerValues As Integer() = {1, 2, 3, 4, 5, 6}
Shared Private integerValuesCopy(6) As Integer
Public Shared Sub Main()
Dim result As Integer
Console.WriteLine("Initial Array Values:" )
PrintArray()
Array.Copy(integerValues, integerValuesCopy,integerValues.Length)
Console.WriteLine("Array values after Copy:" )
PrintArray()
result = Array.BinarySearch(integerValues, 5)
If result >= 0 Then
Console.WriteLine("5 found at element " & result )
Else
Console.WriteLine("5 not found" & " in integerValues")
End If
End Sub
Shared Private Sub PrintArray()
Dim integerElement As Integer
For Each integerElement In integerValues
Console.WriteLine(integerElement )
Next
Console.WriteLine(" integerValuesCopy: ")
For Each integerElement In integerValuesCopy
Console.WriteLine(integerElement )
Next
End Sub
End Class