打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ArrayList.Item 属性的VB.NET例子
下面的代码示例创建一个 ArrayList 并添加多个项。该示例演示如何使用 Item 属性(在 C# 中为索引器)访问元素,并通过为指定索引的 Item 属性赋新值来更改该元素。该示例还演示 Item 属性不能用于在列表的当前大小之外访问或添加元素。
Imports SystemImports System.CollectionsImports Microsoft.VisualBasicPublic Class ExamplePublic Shared Sub Main' Create an empty ArrayList, and add some elements.Dim stringList As New ArrayListstringList.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)TryConsole.WriteLine("Element {0} is ""{1}""", _stringList.Count, _stringList(stringList.Count))Catch aoore As ArgumentOutOfRangeExceptionConsole.WriteLine("stringList({0}) is out of range.", _stringList.Count)End Try' You cannot use the Item property to add new elements.TrystringList(stringList.Count) = "42"Catch aoore As ArgumentOutOfRangeExceptionConsole.WriteLine("stringList({0}) is out of range.", _stringList.Count)End TryConsole.WriteLine()For i As Integer = 0 To stringList.Count - 1Console.WriteLine("Element {0} is ""{1}""", i, stringList(i))NextConsole.WriteLine()For Each o As Object In stringListConsole.WriteLine(o)NextEnd SubEnd Class'' This code example produces the following output:''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
If you believe an article violates your rights or the rights of others, please contact us.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# ArrayList用法 自己写的
C#统计字符串中某字符串出现次数示例
C# IList, ArrayList与List的区别详解 & 简单用法举例(转) - P...
C#下用select方法实现socket服务端
C#中数组Array、ArrayList、泛型List<T>的比较
c#集合类的线程安全
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服