ArrayList::Clear Method ()
Removes all elements from the ArrayList.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| NotSupportedException |
Count is set to zero, and references to other objects from elements of the collection are also released.
Capacity remains unchanged. To reset the capacity of the ArrayList, call TrimToSize or set the Capacity property directly. Trimming an empty ArrayList sets the capacity of the ArrayList to the default capacity.
This method is an O(n) operation, where n is Count.
The following code example shows how to trim the unused portions of the ArrayList and how to clear the values of the ArrayList.
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: */
Available since 10
.NET Framework
Available since 1.1