ArrayList.Clear メソッドとは? わかりやすく解説

ArrayList.Clear メソッド

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

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

NotSupportedException

ArrayList読み取り専用です。

または

ArrayList固定サイズです。

解説解説

Count は 0 に設定されコレクション要素からの他のオブジェクトへの参照解放されます。

Capacity変更されません。ArrayList容量リセットするには、TrimToSize を呼び出すか、Capacity プロパティ直接設定します。空の ArrayListトリムすると、ArrayList容量既定値設定されます。

このメソッドは O(n) 操作です。ここで、nCount です。

使用例使用例

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 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:
 */ 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からArrayList.Clear メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からArrayList.Clear メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からArrayList.Clear メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

ArrayList.Clear メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ArrayList.Clear メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS