ArrayList.Clear 方法

定義

移除 ArrayList 中的所有項目。

public:
 virtual void Clear();
public virtual void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Overridable Sub Clear ()

實作

例外狀況

ArrayList 為唯讀。

-或-

ArrayList 具有固定的大小。

範例

下列程式碼範例示範如何修剪 的未使用部分 ArrayList ,以及如何清除 的值 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( "jumps" );
   
   // 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    jumps
 After TrimToSize,
    Count    : 5
    Capacity : 5
    Values:    The    quick    brown    fox    jumps
 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( "jumps" );

      // 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    jumps
After TrimToSize,
   Count    : 5
   Capacity : 5
   Values:    The    quick    brown    fox    jumps
After Clear,
   Count    : 0
   Capacity : 5
   Values:
After the second TrimToSize,
   Count    : 0
   Capacity : 16
   Values:
*/
Imports System.Collections

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("jumps")
        
        ' 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

End Class

' This code produces the following output.
' 
' Initially,
'    Count    : 5
'    Capacity : 16
'    Values:    The    quick    brown    fox    jumps
' After TrimToSize,
'    Count    : 5
'    Capacity : 5
'    Values:    The    quick    brown    fox    jumps
' After Clear,
'    Count    : 0
'    Capacity : 5
'    Values:
' After the second TrimToSize,
'    Count    : 0
'    Capacity : 16
'    Values:

備註

Count 設為零,同時也發行集合元素的其他物件參照。

Capacity 保持不變。 若要重設 的 ArrayList 容量,請直接呼叫 TrimToSize 或設定 Capacity 屬性。 修剪空白 ArrayList 會將 的 ArrayList 容量設定為預設容量。

這個方法是作業 O(n) ,其中 nCount

適用於

另請參閱