ArrayList 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用大小会根据需要动态增加的数组来实现 IList 接口。
public ref class ArrayList : System::Collections::IListpublic ref class ArrayList : ICloneable, System::Collections::IListpublic class ArrayList : System.Collections.IListpublic class ArrayList : ICloneable, System.Collections.IList[System.Serializable]
public class ArrayList : ICloneable, System.Collections.IList[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArrayList : ICloneable, System.Collections.IListtype ArrayList = class
    interface IEnumerable
    interface IList
    interface ICollectiontype ArrayList = class
    interface ICollection
    interface IEnumerable
    interface IList
    interface ICloneabletype ArrayList = class
    interface IEnumerable
    interface IList
    interface ICollection
    interface ICloneable[<System.Serializable>]
type ArrayList = class
    interface IList
    interface ICollection
    interface IEnumerable
    interface ICloneable[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ArrayList = class
    interface IList
    interface ICollection
    interface IEnumerable
    interface ICloneablePublic Class ArrayList
Implements IListPublic Class ArrayList
Implements ICloneable, IList- 继承
- 
				ArrayList
- 派生
- 属性
- 实现
示例
以下示例演示如何创建和初始化 , ArrayList 以及如何显示其值。
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "Hello" );
   myAL->Add( "World" );
   myAL->Add( "!" );
   
   // Displays the properties and values of the ArrayList.
   Console::WriteLine( "myAL" );
   Console::WriteLine( "    Count:    {0}", myAL->Count );
   Console::WriteLine( "    Capacity: {0}", myAL->Capacity );
   Console::Write( "    Values:" );
   PrintValues( myAL );
}
void PrintValues( IEnumerable^ myList )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "   {0}", obj );
   }
   Console::WriteLine();
}
/* 
This code produces output similar to the following:
myAL
    Count:    3
    Capacity: 4
    Values:   Hello   World   !
*/
using System;
using System.Collections;
public class SamplesArrayList  {
   public static void Main()  {
      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add("Hello");
      myAL.Add("World");
      myAL.Add("!");
      // Displays the properties and values of the ArrayList.
      Console.WriteLine( "myAL" );
      Console.WriteLine( "    Count:    {0}", myAL.Count );
      Console.WriteLine( "    Capacity: {0}", myAL.Capacity );
      Console.Write( "    Values:" );
      PrintValues( myAL );
   }
   public static void PrintValues( IEnumerable myList )  {
      foreach ( Object obj in myList )
         Console.Write( "   {0}", obj );
      Console.WriteLine();
   }
}
/*
This code produces output similar to the following:
myAL
    Count:    3
    Capacity: 4
    Values:   Hello   World   !
*/
Imports System.Collections
Public Class SamplesArrayList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new ArrayList.
        Dim myAL As New ArrayList()
        myAL.Add("Hello")
        myAL.Add("World")
        myAL.Add("!")
        
        ' Displays the properties and values of the ArrayList.
        Console.WriteLine("myAL")
        Console.WriteLine("    Count:    {0}", myAL.Count)
        Console.WriteLine("    Capacity: {0}", myAL.Capacity)
        Console.Write("    Values:")
        PrintValues(myAL)
    End Sub
    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.Write("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub
End Class
' This code produces output similar to the following:
' 
' myAL
'     Count:    3
'     Capacity: 4
'     Values:   Hello   World   !
注解
重要
不建议使用 ArrayList 类进行新开发。 建议改用 泛型 List<T> 类。
类 ArrayList 旨在保存对象的异类集合。 但是,它并不总是提供最佳性能。 相反,我们建议执行以下操作:
- 对于对象的异类集合,请使用 List<Object>C#) 中的 (或List(Of Object)Visual Basic) 类型的 (。
- 对于对象的同质集合,请使用 List<T> 类。 有关这些类的相对性能的讨论,List<T>请参阅参考主题中的性能注意事项。 有关使用 泛型集合类型而不是非泛型集合 类型的常规信息,请参阅不应在 GitHub 上使用非泛型集合。
ArrayList不保证进行排序。 在 (执行操作(例如BinarySearch需要ArrayList排序的) )之前,必须ArrayList通过调用 其 Sort 方法对 进行排序。 若要维护在添加新元素时自动排序的集合,可以使用 SortedSet<T> 类。
的 ArrayList 容量是 可以容纳的元素 ArrayList 数。 将元素添加到 时 ArrayList,容量会根据需要通过重新分配自动增加。 可以通过调用 TrimToSize 或通过显式设置 Capacity 属性来降低容量。
              仅.NET Framework:对于非常大ArrayList的对象,可以通过在运行时环境中将配置元素的 <gcAllowVeryLargeObjects> 属性设置为 enabled ,将 64 位系统上的最大容量增加到 true 20 亿个元素。
可以使用整数索引访问此集合中的元素。 此集合中的索引从零开始。
集合 ArrayList 接受 null 为有效值。 它还允许重复的元素。
不支持将多维数组用作集合中的 ArrayList 元素。
构造函数
| ArrayList() | 初始化 ArrayList 类的新实例,该实例为空并且具有默认初始容量。 | 
| ArrayList(ICollection) | 初始化 ArrayList 类的新实例,该类包含从指定集合复制的元素,并具有与复制的元素数相同的初始容量。 | 
| ArrayList(Int32) | 初始化 ArrayList 类的新实例,该实例为空并且具有指定的初始容量。 | 
属性
| Capacity | 获取或设置 ArrayList 可包含的元素数。 | 
| Count | 获取 ArrayList 中实际包含的元素数。 | 
| IsFixedSize | 获取一个值,该值指示 ArrayList 是否具有固定大小。 | 
| IsReadOnly | 获取一个值,该值指示 ArrayList 是否为只读。 | 
| IsSynchronized | 获取一个值,该值指示是否同步对 ArrayList 的访问(线程安全)。 | 
| Item[Int32] | 获取或设置指定索引处的元素。 | 
| SyncRoot | 获取可用于同步对 ArrayList 的访问的对象。 | 
方法
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 | 
适用于
线程安全性
Visual Basic 中的公共静态 (Shared) 此类型的成员是线程安全的。 但不保证所有实例成员都是线程安全的。
ArrayList只要集合未修改,就可以同时支持多个读取器。 若要保证 的 ArrayList线程安全,所有操作都必须通过 方法返回的 Synchronized(IList) 包装器来完成。
枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。