本文整理匯總了VB.NET中System.Collections.ArrayList.Item屬性的典型用法代碼示例。如果您正苦於以下問題:VB.NET ArrayList.Item屬性的具體用法?VB.NET ArrayList.Item怎麽用?VB.NET ArrayList.Item使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類System.Collections.ArrayList
的用法示例。
在下文中一共展示了ArrayList.Item屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Collections
Public Class Example
Public Shared Sub Main
' Create an empty ArrayList, and add some elements.
Dim stringList As New ArrayList
stringList.Add("a")
stringList.Add("abc")
stringList.Add("abcdef")
stringList.Add("abcdefg")
' Item is the default property, so the property name is
' not required.
Console.WriteLine("Element {0} is ""{1}""", 2, stringList(2))
' Assigning a value to the property changes the value of
' the indexed element.
stringList(2) = "abcd"
Console.WriteLine("Element {0} is ""{1}""", 2, stringList(2))
' Accessing an element outside the current element count
' causes an exception. The ArrayList index is zero-based,
' so the index of the last element is (Count - 1).
Console.WriteLine("Number of elements in the list: {0}", _
stringList.Count)
Try
Console.WriteLine("Element {0} is ""{1}""", _
stringList.Count, _
stringList(stringList.Count))
Catch aoore As ArgumentOutOfRangeException
Console.WriteLine("stringList({0}) is out of range.", _
stringList.Count)
End Try
' You cannot use the Item property to add new elements.
Try
stringList(stringList.Count) = "42"
Catch aoore As ArgumentOutOfRangeException
Console.WriteLine("stringList({0}) is out of range.", _
stringList.Count)
End Try
Console.WriteLine()
For i As Integer = 0 To stringList.Count - 1
Console.WriteLine("Element {0} is ""{1}""", i, stringList(i))
Next
Console.WriteLine()
For Each o As Object In stringList
Console.WriteLine(o)
Next
End Sub
End Class
輸出:
Element 2 is "abcdef" Element 2 is "abcd" Number of elements in the list: 4 stringList(4) is out of range. stringList(4) is out of range. Element 0 is "a" Element 1 is "abc" Element 2 is "abcd" Element 3 is "abcdefg" a abc abcd abcdefg
示例2: ScrambleList
' 導入命名空間
Imports System.Collections
Public Class ScrambleList
Inherits ArrayList
Public Shared Sub Main()
' Create an empty ArrayList, and add some elements.
Dim integerList As New ScrambleList()
For i As Integer = 0 To 9
integerList.Add(i)
Next i
Console.WriteLine("Ordered:" + vbNewLine)
For Each value As Integer In integerList
Console.Write("{0}, ", value)
Next value
Console.WriteLine("<end>" + vbNewLine + vbNewLine + "Scrambled:" + vbNewLine)
' Scramble the order of the items in the list.
integerList.Scramble()
For Each value As Integer In integerList
Console.Write("{0}, ", value)
Next value
Console.WriteLine("<end>" + vbNewLine)
End Sub
Public Sub Scramble()
Dim limit As Integer = MyClass.Count
Dim temp As Integer
Dim swapindex As Integer
Dim rnd As New Random()
For i As Integer = 0 To limit - 1
' The Item property of ArrayList is the default indexer. Thus,
' Me(i) and MyClass.Item(i) are used interchangeably.
temp = CType(Me(i), Integer)
swapindex = rnd.Next(0, limit - 1)
MyClass.Item(i) = Me(swapindex)
MyClass.Item(swapindex) = temp
Next i
End Sub
End Class
輸出:
Ordered: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,Scrambled: 5, 2, 8, 9, 6, 1, 7, 0, 4, 3,