次の方法で共有


ArrayList.Clear メソッド

ArrayList からすべての要素を削除します。

Public Overridable Sub Clear() Implements IList.Clear
[C#]
public virtual void Clear();
[C++]
public: virtual void Clear();
[JScript]
public function Clear();

実装

IList.Clear

例外

例外の種類 条件
NotSupportedException ArrayList が読み取り専用です。

または

ArrayList が固定サイズです。

解説

Count は 0 に設定されます。 Capacity は変更されません。 ArrayList の容量をリセットするには、 TrimToSize を呼び出すか、 Capacity プロパティを直接設定します。空の ArrayList に対して TrimToSize メソッドを使用すると、 ArrayList の容量は 0 ではなく、既定値に設定されます。

使用例

ArrayList の未使用部分をなくす方法と、 ArrayList の値を消去する方法の例を次に示します。

 
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 myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
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:

[C#] 
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 )  {
      System.Collections.IEnumerator myEnumerator = 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:
*/ 

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void PrintValues( IEnumerable* myList );
 
void main()  {
 
       // Creates and initializes a new ArrayList.
       ArrayList* myAL = new ArrayList();
       myAL->Add( S"The" );
       myAL->Add( S"quick" );
       myAL->Add( S"brown" );
       myAL->Add( S"fox" );
       myAL->Add( S"jumped" );
 
       // Displays the count, capacity and values of the ArrayList.
       Console::WriteLine( "Initially," );
       Console::WriteLine( "   Count    : {0}", __box(myAL->Count) );
       Console::WriteLine( "   Capacity : {0}", __box(myAL->Capacity) );
       Console::Write( "   Values:" );
       PrintValues( myAL );
 
       // Trims the ArrayList.
       myAL->TrimToSize();
 
       // Displays the count, capacity and values of the ArrayList.
       Console::WriteLine( "After TrimToSize," );
       Console::WriteLine( "   Count    : {0}", __box(myAL->Count) );
       Console::WriteLine( "   Capacity : {0}", __box(myAL->Capacity) );
       Console::Write( "   Values:" );
       PrintValues( myAL );
 
       // Clears the ArrayList.
       myAL->Clear();
 
       // Displays the count, capacity and values of the ArrayList.
       Console::WriteLine( "After Clear," );
       Console::WriteLine( "   Count    : {0}", __box(myAL->Count) );
       Console::WriteLine( "   Capacity : {0}", __box(myAL->Capacity) );
       Console::Write( "   Values:" );
       PrintValues( myAL );
 
       // Trims the ArrayList again.
       myAL->TrimToSize();
 
       // Displays the count, capacity and values of the ArrayList.
       Console::WriteLine( "After the second TrimToSize," );
       Console::WriteLine( "   Count    : {0}", __box(myAL->Count) );
       Console::WriteLine( "   Capacity : {0}", __box(myAL->Capacity) );
       Console::Write( "   Values:" );
       PrintValues( myAL );
}
 
void PrintValues( IEnumerable* myList )  {
       System::Collections::IEnumerator* myEnumerator = 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:
 */ 

[JScript] 
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:
 */ 

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

ArrayList クラス | ArrayList メンバ | System.Collections 名前空間 | TrimToSize | Capacity | Count