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