ArrayList-Klasse

Implementiert die IList-Schnittstelle unter Verwendung eines Arrays, das nach Bedarf dynamisch vergrößert wird.

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

Syntax

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class ArrayList
    Implements IList, ICollection, IEnumerable, ICloneable
'Usage
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
SerializableAttribute 
ComVisibleAttribute(true) 
public class ArrayList implements IList, ICollection, 
    IEnumerable, ICloneable

Hinweise

Es wird nicht sichergestellt, dass die ArrayList sortiert ist. Sie müssen die ArrayList sortieren, bevor Sie Vorgänge wie BinarySearch durchführen, die eine sortierte ArrayList voraussetzen.

Bei der Kapazität einer ArrayList handelt es sich um die Anzahl der Elemente, die die ArrayList enthalten kann. Die anfängliche Standardkapazität für eine ArrayList ist 0. Beim Hinzufügen von Elementen zur ArrayList wird die Kapazität durch Neureservierung automatisch nach Bedarf erhöht. Die Kapazität kann über einen Aufruf von TrimToSize oder durch ein explizites Festlegen der Capacity-Eigenschaft verringert werden.

Auf Elemente in dieser Auflistung kann mithilfe eines ganzzahligen Index zugegriffen werden. Diese Auflistung verwendet nullbasierte Indizes.

ArrayList akzeptiert NULL (Nothing in Visual Basic) als gültigen Wert und lässt doppelte Elemente zu.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie eine ArrayList erstellt und initialisiert wird und wie die entsprechenden Werte ausgegeben werden können.

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

Vererbungshierarchie

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

Threadsicherheit

Öffentliche statische (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

ArrayList stellt eine gleichzeitige Unterstützung für mehrere Reader bereit, sofern die Auflistung nicht verändert wird. Zur Gewährleistung der Threadsicherheit von ArrayList müssen alle Operationen unter Verwendung des Wrappers ausgeführt werden, der von der Synchronized-Methode zurückgegeben wird.

Die Enumeration einer Auflistung ist systemintern keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert wird, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Um während der Enumeration Threadsicherheit zu gewährleisten, können Sie entweder die Auflistung während der gesamten Enumeration sperren, oder Sie können die durch andere Threads aufgrund von Änderungen ausgelösten Ausnahmen abfangen.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

ArrayList-Member
System.Collections-Namespace
IList
System.Collections.Generic.List

Weitere Ressourcen

Durchführen kulturunabhängiger Zeichenfolgenoperationen in Auflistungen