ArrayList 类

定义

使用大小会根据需要动态增加的数组来实现 IList 接口。

public ref class ArrayList : System::Collections::IList
public ref class ArrayList : ICloneable, System::Collections::IList
public class ArrayList : System.Collections.IList
public 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.IList
type ArrayList = class
    interface IEnumerable
    interface IList
    interface ICollection
type ArrayList = class
    interface ICollection
    interface IEnumerable
    interface IList
    interface ICloneable
type 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 ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ArrayList = class
    interface IList
    interface ICloneable
    interface ICollection
    interface IEnumerable
Public Class ArrayList
Implements IList
Public 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 的访问的对象。

方法

Adapter(IList)

为特定 IList 创建 ArrayList 包装。

Add(Object)

将对象添加到 ArrayList 的结尾处。

AddRange(ICollection)

ICollection 的元素添加到 ArrayList 的末尾。

BinarySearch(Int32, Int32, Object, IComparer)

使用指定的比较器在已排序 ArrayList 的某个元素范围中搜索元素,并返回该元素从零开始的索引。

BinarySearch(Object)

使用默认的比较器在整个已排序的 ArrayList 中搜索元素,并返回该元素从零开始的索引。

BinarySearch(Object, IComparer)

使用指定的比较器在整个已排序的 ArrayList 中搜索元素,并返回该元素从零开始的索引。

Clear()

ArrayList 中移除所有元素。

Clone()

创建 ArrayList 的浅表副本。

Contains(Object)

确定某元素是否在 ArrayList 中。

CopyTo(Array)

从目标数组的开头开始,将整个 ArrayList 复制到兼容的一维 Array

CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 ArrayList 复制到兼容的一维 Array

CopyTo(Int32, Array, Int32, Int32)

从目标数组的指定索引处开始,将 ArrayList 中某个范围的元素复制到兼容的一维数组 Array

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
FixedSize(ArrayList)

返回具有固定大小的 ArrayList 包装。

FixedSize(IList)

返回具有固定大小的 IList 包装。

GetEnumerator()

返回用于整个 ArrayList 的枚举数。

GetEnumerator(Int32, Int32)

返回 ArrayList 中元素范围的枚举器。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetRange(Int32, Int32)

返回一个 ArrayList,它表示源 ArrayList 中的元素子集。

GetType()

获取当前实例的 Type

(继承自 Object)
IndexOf(Object)

搜索指定的 Object,并返回整个 ArrayList 中第一个匹配项的从零开始的索引。

IndexOf(Object, Int32)

搜索指定的 Object,并返回 ArrayList 中从指定索引到最后一个元素的元素范围中第一个匹配项的从零开始索引。

IndexOf(Object, Int32, Int32)

搜索指定的 Object,并返回 ArrayList 中从指定索引开始,并包含指定元素数的元素范围中第一个匹配项的从零开始的索引。

Insert(Int32, Object)

将元素插入 ArrayList 的指定索引处。

InsertRange(Int32, ICollection)

将集合中的元素插入 ArrayList 的指定索引处。

LastIndexOf(Object)

在整个 ArrayList 中搜索指定的 Object,并返回最后一个匹配项的从零开始的索引。

LastIndexOf(Object, Int32)

搜索指定的 Object,并返回 ArrayList 中从第一个元素到指定索引这部分元素中最后一个匹配项的从零开始索引。

LastIndexOf(Object, Int32, Int32)

搜索指定的 Object,并返回 ArrayList 中到指定索引为止包含指定元素数的这部分元素中最后一个匹配项的从零开始的索引。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ReadOnly(ArrayList)

返回只读的 ArrayList 包装。

ReadOnly(IList)

返回只读的 IList 包装。

Remove(Object)

ArrayList 中移除特定对象的第一个匹配项。

RemoveAt(Int32)

移除 ArrayList 的指定索引处的元素。

RemoveRange(Int32, Int32)

ArrayList 中移除一系列元素。

Repeat(Object, Int32)

返回 ArrayList,其元素是指定值的副本。

Reverse()

将整个 ArrayList 中元素的顺序反转。

Reverse(Int32, Int32)

将指定范围中元素的顺序反转。

SetRange(Int32, ICollection)

复制 ArrayList 中一个子集合的元素。

Sort()

对整个 ArrayList 中的元素进行排序。

Sort(IComparer)

使用指定的比较器对整个 ArrayList 中的元素进行排序。

Sort(Int32, Int32, IComparer)

使用指定的比较器对 ArrayList 中某个范围内的元素进行排序。

Synchronized(ArrayList)

返回同步的(线程安全)ArrayList 包装器。

Synchronized(IList)

返回同步的(线程安全)IList 包装器。

ToArray()

ArrayList 的元素复制到新 Object 数组中。

ToArray(Type)

ArrayList 的元素复制到新的指定元素类型数组中。

ToString()

返回表示当前对象的字符串。

(继承自 Object)
TrimToSize()

将容量设置为 ArrayList 中元素的实际数目。

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

线程安全性

Visual Basic 中的公共静态 (Shared) 此类型的成员是线程安全的。 但不保证所有实例成员都是线程安全的。

ArrayList只要集合未修改,就可以同时支持多个读取器。 若要保证 的 ArrayList线程安全,所有操作都必须通过 方法返回的 Synchronized(IList) 包装器来完成。

枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。

另请参阅