本文整理匯總了VB.NET中System.Array.LastIndexOf方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Array.LastIndexOf方法的具體用法?VB.NET Array.LastIndexOf怎麽用?VB.NET Array.LastIndexOf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Array
的用法示例。
在下文中一共展示了Array.LastIndexOf方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Main
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array with three elements of
' the same value.
Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
myArray.SetValue("the", 0)
myArray.SetValue("quick", 1)
myArray.SetValue("brown", 2)
myArray.SetValue("fox", 3)
myArray.SetValue("jumps", 4)
myArray.SetValue("over", 5)
myArray.SetValue("the", 6)
myArray.SetValue("lazy", 7)
myArray.SetValue("dog", 8)
myArray.SetValue("in", 9)
myArray.SetValue("the", 10)
myArray.SetValue("barn", 11)
' Displays the values of the Array.
Console.WriteLine("The Array contains the following values:")
PrintIndexAndValues(myArray)
' Searches for the last occurrence of the duplicated value.
Dim myString As String = "the"
Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first
' section of the Array.
myIndex = Array.LastIndexOf(myArray, myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start " _
+ "and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section
' of the Array. Note that the start index is greater than the end
' index because the search is done backward.
myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
+ "and index 10 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myArray As Array)
Dim i As Integer
For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myArray.GetValue(i))
Next i
End Sub
End Class
輸出:
The Array contains the following values: [0]: the [1]: quick [2]: brown [3]: fox [4]: jumps [5]: over [6]: the [7]: lazy [8]: dog [9]: in [10]: the [11]: barn The last occurrence of "the" is at index 10. The last occurrence of "the" between the start and index 8 is at index 6. The last occurrence of "the" between index 5 and index 10 is at index 10.
示例2: Example
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
輸出:
Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5 Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0 Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
示例3: MainClass
' 導入命名空間
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim values As Array = _
Array.CreateInstance(GetType(Integer), 11)
For i As Integer = 0 To 10
values.SetValue(i, i)
Next i
Console.WriteLine(Array.IndexOf(values, 6).ToString)
Console.WriteLine(Array.LastIndexOf(values, 3).ToString)
End Sub
End Class