31 out of 78 rated this helpful - Rate this topic

ArrayList Class

Implements the IList interface using an array whose size is dynamically increased as required.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class ArrayList : IList, ICollection, IEnumerable, 
	ICloneable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class ArrayList implements IList, ICollection, 
	IEnumerable, ICloneable
SerializableAttribute 
ComVisibleAttribute(true) 
public class ArrayList implements IList, ICollection, 
	IEnumerable, ICloneable
Not applicable.

The ArrayList is not guaranteed to be sorted. You must sort the ArrayList prior to performing operations (such as BinarySearch) that require the ArrayList to be sorted.

The capacity of a ArrayList is the number of elements the ArrayList can hold. As elements are added to a ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly.

Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

ArrayList accepts a null reference (Nothing in Visual Basic) as a valid value and allows duplicate elements.

The following code example shows how to create and initialize an ArrayList and how to print out its values.

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   !

*/

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    !
 */

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

An ArrayList can support multiple readers concurrently, as long as the collection is not modified. To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the Synchronized method.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Missing closing curly brace
In many code snippets at MSDN, the closing curly brace is unfortunately missing. This would mislead people trying to learn these languages for the first time. So people get help from experts while learning.
Look at List<T> class instead

If you are targeting the .NET Framework 2.0 or higher, have a look at the generic List<T> class. Unlike ArrayList, it is strongly typed and prevents boxing for value types (structures). It has also had a number of performance improvements over its predecessor.

The most popular collection class -- be careful in perf critical situations

In Visual Studio 2005 consider using List<T> anywhere you might have otherwise used ArrayList as it offers superior performance in almost every situation. In particular foreach (...) over an ArrayList is much slower than the equivalent operation over List<T>. In performance critical situations consider using for (...) instead of foreach (...) when iterating over an ArrayList.

See List<T> and the comments there