ArrayList.Clear-Methode
Entfernt alle Elemente aus der ArrayList.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Overridable Sub Clear
'Usage
Dim instance As ArrayList
instance.Clear
public virtual void Clear ()
public:
virtual void Clear ()
public void Clear ()
public function Clear ()
Ausnahmen
Ausnahmetyp | Bedingung |
---|---|
ArrayList ist schreibgeschützt. – oder – ArrayList hat eine feste Größe. |
Hinweise
Count wird auf 0 (null) festgelegt, und Verweise auf andere Objekte von Elementen in der Auflistung werden ebenfalls freigegeben.
Capacity bleibt unverändert. Wenn die Kapazität von ArrayList zurückgesetzt werden soll, rufen Sie TrimToSize auf, oder legen Sie die Capacity-Eigenschaft direkt fest. Durch das Verkürzen einer leeren ArrayList wird die Kapazität der ArrayList auf die Standardkapazität festgelegt.
Diese Methode ist eine O(n)-Operation, wobei n der Count ist.
Beispiel
Im folgenden Codebeispiel wird veranschaulicht, wie nicht verwendete Bereiche von ArrayList verkürzt und die Werte in ArrayList gelöscht werden.
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("The")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumped")
' Displays the count, capacity and values of the ArrayList.
Console.WriteLine("Initially,")
Console.WriteLine(" Count : {0}", myAL.Count)
Console.WriteLine(" Capacity : {0}", myAL.Capacity)
Console.Write(" Values:")
PrintValues(myAL)
' Trim the ArrayList.
myAL.TrimToSize()
' Displays the count, capacity and values of the ArrayList.
Console.WriteLine("After TrimToSize,")
Console.WriteLine(" Count : {0}", myAL.Count)
Console.WriteLine(" Capacity : {0}", myAL.Capacity)
Console.Write(" Values:")
PrintValues(myAL)
' Clear the ArrayList.
myAL.Clear()
' Displays the count, capacity and values of the ArrayList.
Console.WriteLine("After Clear,")
Console.WriteLine(" Count : {0}", myAL.Count)
Console.WriteLine(" Capacity : {0}", myAL.Capacity)
Console.Write(" Values:")
PrintValues(myAL)
' Trim the ArrayList again.
myAL.TrimToSize()
' Displays the count, capacity and values of the ArrayList.
Console.WriteLine("After the second TrimToSize,")
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 the following output.
'
' Initially,
' Count : 5
' Capacity : 16
' Values: The quick brown fox jumped
' After TrimToSize,
' Count : 5
' Capacity : 5
' Values: The quick brown fox jumped
' After Clear,
' Count : 0
' Capacity : 5
' Values:
' After the second TrimToSize,
' Count : 0
' Capacity : 16
' 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( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "Initially," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
// Trim the ArrayList.
myAL.TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After TrimToSize," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
// Clear the ArrayList.
myAL.Clear();
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After Clear," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
// Trim the ArrayList again.
myAL.TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After the second TrimToSize," );
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 the following output.
Initially,
Count : 5
Capacity : 16
Values: The quick brown fox jumped
After TrimToSize,
Count : 5
Capacity : 5
Values: The quick brown fox jumped
After Clear,
Count : 0
Capacity : 5
Values:
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
*/
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( "The" );
myAL->Add( "quick" );
myAL->Add( "brown" );
myAL->Add( "fox" );
myAL->Add( "jumped" );
// Displays the count, capacity and values of the ArrayList.
Console::WriteLine( "Initially," );
Console::WriteLine( " Count : {0}", myAL->Count );
Console::WriteLine( " Capacity : {0}", myAL->Capacity );
Console::Write( " Values:" );
PrintValues( myAL );
// Trim the ArrayList.
myAL->TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console::WriteLine( "After TrimToSize," );
Console::WriteLine( " Count : {0}", myAL->Count );
Console::WriteLine( " Capacity : {0}", myAL->Capacity );
Console::Write( " Values:" );
PrintValues( myAL );
// Clear the ArrayList.
myAL->Clear();
// Displays the count, capacity and values of the ArrayList.
Console::WriteLine( "After Clear," );
Console::WriteLine( " Count : {0}", myAL->Count );
Console::WriteLine( " Capacity : {0}", myAL->Capacity );
Console::Write( " Values:" );
PrintValues( myAL );
// Trim the ArrayList again.
myAL->TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console::WriteLine( "After the second TrimToSize," );
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 the following output.
Initially,
Count : 5
Capacity : 16
Values: The quick brown fox jumped
After TrimToSize,
Count : 5
Capacity : 5
Values: The quick brown fox jumped
After Clear,
Count : 0
Capacity : 5
Values:
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
*/
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("The");
myAL.Add("quick");
myAL.Add("brown");
myAL.Add("fox");
myAL.Add("jumped");
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine("Initially,");
Console.WriteLine(" Count : {0}", (Int32)myAL.get_Count());
Console.WriteLine(" Capacity : {0}", (Int32)myAL.get_Capacity());
Console.Write(" Values:");
PrintValues(myAL);
// Trim the ArrayList.
myAL.TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine("After TrimToSize,");
Console.WriteLine(" Count : {0}", (Int32)myAL.get_Count());
Console.WriteLine(" Capacity : {0}", (Int32)myAL.get_Capacity());
Console.Write(" Values:");
PrintValues(myAL);
// Clear the ArrayList.
myAL.Clear();
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine("After Clear,");
Console.WriteLine(" Count : {0}", (Int32)myAL.get_Count());
Console.WriteLine(" Capacity : {0}", (Int32)myAL.get_Capacity());
Console.Write(" Values:");
PrintValues(myAL);
// Trim the ArrayList again.
myAL.TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine("After the second TrimToSize,");
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 the following output.
Initially,
Count : 5
Capacity : 16
Values: The quick brown fox jumped
After TrimToSize,
Count : 5
Capacity : 5
Values: The quick brown fox jumped
After Clear,
Count : 0
Capacity : 5
Values:
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
*/
import System;
import System.Collections;
// Creates and initializes a new ArrayList.
var myAL : ArrayList = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "Initially," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
// Trim the ArrayList.
myAL.TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After TrimToSize," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
// Clear the ArrayList.
myAL.Clear();
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After Clear," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
// Trim the ArrayList again.
myAL.TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After the second TrimToSize," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
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.
Initially,
Count : 5
Capacity : 16
Values: The quick brown fox jumped
After TrimToSize,
Count : 5
Capacity : 5
Values: The quick brown fox jumped
After Clear,
Count : 0
Capacity : 5
Values:
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
*/
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-Klasse
ArrayList-Member
System.Collections-Namespace
TrimToSize
Capacity
Count