更新:2007 年 11 月
使用大小可按需动态增加的数组实现 IList 接口。
命名空间: System.Collections
程序集: mscorlib(在 mscorlib.dll 中)
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
不保证会对 ArrayList 排序。 在执行需要对 ArrayList 排序的操作(如 BinarySearch)之前,必须对 ArrayList 进行排序。
ArrayList 的容量是 ArrayList 可以保存的元素数。随着向 ArrayList 中添加元素,容量通过重新分配按需自动增加。可通过调用 TrimToSize 或通过显式设置 Capacity 属性减少容量。
使用整数索引可以访问此集合中的元素。 此集合中的索引从零开始。
ArrayList 接受 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 作为有效值并且允许重复的元素。
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
下面的代码示例演示如何创建并初始化 ArrayList 以及如何打印出其值。
Imports System Imports System.Collections Imports Microsoft.VisualBasic 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 'PrintValues End Class ' 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: f Values: Hello World ! */
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 ! */
import System.*; import System.Collections.*; public class SamplesArrayList { public static void main(String[] args) { // 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}", (Int32)myAL.get_Count()); Console.WriteLine(" Capacity: {0}", (Int32)myAL.get_Capacity()); Console.Write(" Values:"); PrintValues(myAL); } //main public static void PrintValues(IEnumerable myList) { IEnumerator objMyEnum = myList.GetEnumerator(); while (objMyEnum.MoveNext()) { Object obj = objMyEnum.get_Current(); Console.Write(" {0}", obj); } Console.WriteLine(); } //PrintValues } //SamplesArrayList /* This code produces output similar to the following: myAL Count: 3 Capacity: 4 Values: Hello World ! */
import System; import System.Collections; // Creates and initializes a new ArrayList. var myAL : ArrayList = new ArrayList(); myAL.Add("Hello"); myAL.Add("World"); myAL.Add("!"); // Displays the properties and values of the ArrayList. Console.WriteLine( "myAL" ); Console.WriteLine( "/tCount: {0}", myAL.Count ); Console.WriteLine( "/tCapacity: {0}", myAL.Capacity ); Console.Write( "/tValues:" ); PrintValues( myAL ); function PrintValues( myList : IEnumerable ) { var myEnumerator : System.Collections.IEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "/t{0}", myEnumerator.Current ); Console.WriteLine(); } /* This code produces output similar to the following: myAL Count: 3 Capacity: 4 Values: Hello World ! */
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
此类型的公共静态(在 Visual Basic 中为 Shared)成员是线程安全的。但不能保证任何实例成员是线程安全的。
只要集合未修改,ArrayList 就可以同时支持多个阅读器。若要保证 ArrayList 的线程安全,则必须通过由 Synchronized 方法返回的包装来执行所有操作。
从头到尾对一个集合进行枚举在本质上不是一个线程安全的过程。即使一个集合已进行同步,其他线程仍可以修改该集合,这将导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public Class ArrayList _ Implements IList, ICollection, IEnumerable, ICloneable
Dim instance As ArrayList
[SerializableAttribute] [ComVisibleAttribute(true)] public class ArrayList : IList, ICollection, IEnumerable, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class ArrayList : IList, ICollection, IEnumerable, ICloneable
/** @attribute SerializableAttribute */ /** @attribute ComVisibleAttribute(true) */ public class ArrayList implements IList, ICollection, IEnumerable, ICloneable
public class ArrayList implements IList, ICollection, IEnumerable, ICloneable