ArrayList.Item 属性

获取或设置指定索引处的元素。

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable Default Property Item ( _
    index As Integer _
) As Object
用法
Dim instance As ArrayList
Dim index As Integer
Dim value As Object

value = instance(index)

instance(index) = value
public virtual Object this [
    int index
] { get; set; }
public:
virtual property Object^ default [int] {
    Object^ get (int index);
    void set (int index, Object^ value);
}
/** @property */
public Object get_Item (int index)

/** @property */
public void set_Item (int index, Object value)
JScript 支持使用索引属性,但不支持进行新的声明。

参数

  • index
    要获得或设置的元素从零开始的索引。

属性值

指定索引处的元素。

异常

异常类型 条件

ArgumentOutOfRangeException

index 小于零。

- 或 -

index 等于或大于 Count

备注

ArrayList 接受 空引用(在 Visual Basic 中为 Nothing) 作为有效值并且允许重复的元素。

通过使用下面的语法,此属性提供访问集合中特定元素的能力:myCollection[index]

检索此属性值的运算复杂度为 O(1);设置此属性的运算复杂度也是 O(1)。

示例

下面的代码示例创建一个 ArrayList 并添加多个项。该示例演示如何使用 Item 属性(在 C# 中为索引器)访问元素,并通过为指定索引的 Item 属性赋新值来更改该元素。该示例还演示 Item 属性不能用于在列表的当前大小之外访问或添加元素。

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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
'
' 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
using System;
using System.Collections;

public class Example
{
    public static void Main()
    {
        // Create an empty ArrayList, and add some elements.
        ArrayList stringList = new ArrayList();

        stringList.Add("a");
        stringList.Add("abc");
        stringList.Add("abcdef");
        stringList.Add("abcdefg");

        // The Item property is an indexer, 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. 
        Console.WriteLine("Number of elements in the list: {0}", 
            stringList.Count);
        try
        {
            Console.WriteLine("Element {0} is \"{1}\"", 
                stringList.Count, stringList[stringList.Count]);
        }
        catch(ArgumentOutOfRangeException aoore)
        {
            Console.WriteLine("stringList({0}) is out of range.", 
                stringList.Count);
        }

        // You cannot use the Item property to add new elements.
        try
        {
            stringList[stringList.Count] = "42";
        }
        catch(ArgumentOutOfRangeException aoore)
        {
            Console.WriteLine("stringList({0}) is out of range.", 
                stringList.Count);
        }

        Console.WriteLine();
        for (int i = 0; i < stringList.Count; i++)
        {
            Console.WriteLine("Element {0} is \"{1}\"", i, 
                stringList[i]);
        }

        Console.WriteLine();
        foreach (object o in stringList)
        {
            Console.WriteLine(o);
        }
    }
}
/*
 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
 */

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ArrayList 类
ArrayList 成员
System.Collections 命名空间
ArrayList.Count 属性