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가 0으로 설정되어 있고 컬렉션 요소에서 다른 개체로의 참조도 해제되어 있습니다.

Capacity 변경되지 않은 상태로 유지됩니다. 의 ArrayList용량을 다시 설정하려면 속성을 직접 호출 TrimToSize 하거나 설정합니다 Capacity . 빈 ArrayList 값을 트리밍하면 의 ArrayList 용량이 기본 용량으로 설정됩니다.

이 메서드는 작업입니다 O(n) . 여기서 n 는 입니다 Count.

적용 대상

추가 정보