Export (0) Print
Expand All
58 out of 112 rated this helpful - Rate this topic

ArrayList Class

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

For a list of all members of this type, see ArrayList Members.

System.Object
   System.Collections.ArrayList
      System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection

[Visual Basic]
<Serializable>
Public Class ArrayList
   Implements IList, ICollection, IEnumerable, ICloneable
[C#]
[Serializable]
public class ArrayList : IList, ICollection, IEnumerable,
   ICloneable
[C++]
[Serializable]
public __gc class ArrayList : public IList, ICollection,
   IEnumerable, ICloneable
[JScript]
public
   Serializable
class ArrayList implements IList, ICollection, IEnumerable,
   ICloneable

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. 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 could 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.

Remarks

The capacity of an ArrayList is the number of elements the list can hold. As elements are added to an 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.

Indexes in this collection are zero-based.

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

Example

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

[Visual Basic] 
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(ControlChars.Tab + "Count:    {0}", myAL.Count)
        Console.WriteLine(ControlChars.Tab + "Capacity: {0}", myAL.Capacity)
        Console.Write(ControlChars.Tab + "Values:")
        PrintValues(myAL)
    End Sub
    
    
    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' myAL
'     Count:    3
'     Capacity: 16
'     Values:    Hello    World    ! 

[C#] 
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( "\tCount:    {0}", myAL.Count );
      Console.WriteLine( "\tCapacity: {0}", myAL.Capacity );
      Console.Write( "\tValues:" );
      PrintValues( myAL );
   }

   public static void PrintValues( IEnumerable myList )  {
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "\t{0}", myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

myAL
    Count:    3
    Capacity: 16
    Values:    Hello    World    !
*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void PrintValues( IEnumerable* myList );
 
void main()  {
 
    // Creates and initializes a new ArrayList.
    ArrayList* myAL = new ArrayList();
    myAL->Add(S"Hello");
    myAL->Add(S"World");
    myAL->Add(S"!");
 
    // Displays the properties and values of the ArrayList.
    Console::WriteLine( "myAL" );
    Console::WriteLine( "\tCount:    {0}", __box(myAL->Count) );
    Console::WriteLine( "\tCapacity: {0}", __box(myAL->Capacity) );
    Console::Write( "\tValues:" );
    PrintValues( myAL );
}
 
void PrintValues( IEnumerable* myList ) {
    System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
    while ( myEnumerator->MoveNext() )
       Console::Write( "\t{0}", myEnumerator->Current );
    Console::WriteLine();
}

 /* 
 This code produces the following output.
 
 myAL
     Count:    3
     Capacity: 16
     Values:    Hello    World    !
 */

[JScript] 
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 the following output.
 
 myAL
     Count:    3
     Capacity: 16
     Values:    Hello    World    !
 */

Requirements

Namespace: System.Collections

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

ArrayList Members | System.Collections Namespace | IList | Performing Culture-Insensitive String Operations in Collections

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.