本文整理汇总了VB.NET中System.Collections.ArrayList.IndexOf方法的典型用法代码示例。如果您正苦于以下问题:VB.NET ArrayList.IndexOf方法的具体用法?VB.NET ArrayList.IndexOf怎么用?VB.NET ArrayList.IndexOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.ArrayList
的用法示例。
在下文中一共展示了ArrayList.IndexOf方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: SamplesArrayList
' 导入命名空间
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList with three elements of the same value.
Dim myAL As New ArrayList()
myAL.Add("the")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")
myAL.Add("in")
myAL.Add("the")
myAL.Add("barn")
' Displays the values of the ArrayList.
Console.WriteLine("The ArrayList contains the following values:")
PrintIndexAndValues(myAL)
' Search for the first occurrence of the duplicated value.
Dim myString As [String] = "the"
Dim myIndex As Integer = myAL.IndexOf(myString)
Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.", myString, myIndex)
' Search for the first occurrence of the duplicated value in the last section of the ArrayList.
myIndex = myAL.IndexOf(myString, 4)
Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.", myString, myIndex)
' Search for the first occurrence of the duplicated value in a section of the ArrayList.
myIndex = myAL.IndexOf(myString, 6, 6)
Console.WriteLine("The first occurrence of ""{0}"" between index 6 and index 11 is at index {1}.", myString, myIndex)
' Search for the first occurrence of the duplicated value in a small section at the end of the ArrayList.
myIndex = myAL.IndexOf(myString, 11)
Console.WriteLine("The first occurrence of ""{0}"" between index 11 and the end is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(ByVal myList As IEnumerable)
Dim i As Integer
Dim obj As [Object]
For Each obj In myList
Console.WriteLine(" [{0}]: {1}", i, obj)
i = i + 1
Next obj
Console.WriteLine()
End Sub
End Class
输出:
The ArrayList 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 first occurrence of "the" is at index 0. The first occurrence of "the" between index 4 and the end is at index 6. The first occurrence of "the" between index 6 and index 11 is at index 6. The first occurrence of "the" between index 11 and the end is at index -1.
示例2: MainClass
' 导入命名空间
Imports System
Imports System.Collections
Public Class MainClass
Public Shared Sub Main()
Dim arrayList As arrayList = New arrayList(1)
arrayList.Add("a")
try
Console.WriteLine(arrayList(0))
Catch outOfRange As ArgumentOutOfRangeException
Console.WriteLine(outOfRange.ToString())
End Try
Console.WriteLine(arrayList.Count)
Console.WriteLine(arrayList.Contains("a"))
Console.WriteLine(arrayList.IndexOf("a"))
arrayList.Remove("a")
arrayList.TrimToSize()
Console.WriteLine(arrayList.Capacity)
arrayList.Add("a")
Dim enumerator As IEnumerator = arrayList.GetEnumerator()
While enumerator.MoveNext()
Console.WriteLine(enumerator.Current )
End While
End Sub
End Class