SortedList.Clearとは? わかりやすく解説

SortedList.Clear メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

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

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

解説解説

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

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

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

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SortedList ジェネリック クラス
SortedList メンバ
System.Collections.Generic 名前空間
TrimExcess
Remove
RemoveAt

SortedList.Clear メソッド

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

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

例外例外
例外種類条件

NotSupportedException

SortedList読み取り専用です。

または

SortedList固定サイズです。

解説解説

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

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

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

使用例使用例

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

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesSortedList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New
 SortedList()
        mySL.Add("one", "The")
        mySL.Add("two", "quick")
        mySL.Add("three", "brown")
        mySL.Add("four", "fox")
        mySL.Add("five", "jumped")
        
        ' Displays the count, capacity and values of the SortedList.
        Console.WriteLine("Initially,")
        Console.WriteLine("   Count    : {0}", mySL.Count)
        Console.WriteLine("   Capacity : {0}", mySL.Capacity)
        Console.WriteLine("   Values:")
        PrintKeysAndValues(mySL)
        
        ' Trims the SortedList.
        mySL.TrimToSize()
        
        ' Displays the count, capacity and values of the SortedList.
        Console.WriteLine("After TrimToSize,")
        Console.WriteLine("   Count    : {0}", mySL.Count)
        Console.WriteLine("   Capacity : {0}", mySL.Capacity)
        Console.WriteLine("   Values:")
        PrintKeysAndValues(mySL)
        
        ' Clears the SortedList.
        mySL.Clear()
        
        ' Displays the count, capacity and values of the SortedList.
        Console.WriteLine("After Clear,")
        Console.WriteLine("   Count    : {0}", mySL.Count)
        Console.WriteLine("   Capacity : {0}", mySL.Capacity)
        Console.WriteLine("   Values:")
        PrintKeysAndValues(mySL)
        
        ' Trims the SortedList again.
        mySL.TrimToSize()
        
        ' Displays the count, capacity and values of the SortedList.
        Console.WriteLine("After the second TrimToSize,")
        Console.WriteLine("   Count    : {0}", mySL.Count)
        Console.WriteLine("   Capacity : {0}", mySL.Capacity)
        Console.WriteLine("   Values:")
        PrintKeysAndValues(mySL)
    End Sub   
    
    
    Public Shared Sub PrintKeysAndValues(myList
 As SortedList)
        Console.WriteLine(ControlChars.Tab & "-KEY-"
 & ControlChars.Tab & _
           "-VALUE-")
        Dim i As Integer
        For i = 0 To myList.Count - 1
            Console.WriteLine(ControlChars.Tab & "{0}:"
 & ControlChars.Tab & _
               "{1}", myList.GetKey(i), myList.GetByIndex(i))
        Next i
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Initially,
'    Count    : 5
'    Capacity : 16
'    Values:
'     -KEY-    -VALUE-
'     five:    jumped
'     four:    fox
'     one:    The
'     three:    brown
'     two:    quick
'
' After TrimToSize,
'    Count    : 5
'    Capacity : 5
'    Values:
'     -KEY-    -VALUE-
'     five:    jumped
'     four:    fox
'     one:    The
'     three:    brown
'     two:    quick
' 
' After Clear,
'    Count    : 0
'    Capacity : 16
'    Values:
'     -KEY-    -VALUE-
' 
'
' After the second TrimToSize,
'    Count    : 0
'    Capacity : 16
'    Values:
'     -KEY-    -VALUE-
 
using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()
  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( "one", "The" );
      mySL.Add( "two", "quick" );
      mySL.Add( "three", "brown" );
      mySL.Add( "four", "fox" );
      mySL.Add( "five", "jumped" );

      // Displays the count, capacity and values of the SortedList.
      Console.WriteLine( "Initially," );
      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( mySL );

      // Trims the SortedList.
      mySL.TrimToSize();

      // Displays the count, capacity and values of the SortedList.
      Console.WriteLine( "After TrimToSize," );
      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( mySL );

      // Clears the SortedList.
      mySL.Clear();

      // Displays the count, capacity and values of the SortedList.
      Console.WriteLine( "After Clear," );
      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( mySL );

      // Trims the SortedList again.
      mySL.TrimToSize();

      // Displays the count, capacity and values of the SortedList.
      Console.WriteLine( "After the second TrimToSize," );
      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( mySL );
   }


   public static void PrintKeysAndValues(
 SortedList myList )  {
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < myList.Count;
 i++ )  {
         Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i)
 );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Initially,
   Count    : 5
   Capacity : 16
   Values:
    -KEY-    -VALUE-
    five:    jumped
    four:    fox
    one:    The
    three:    brown
    two:    quick

After TrimToSize,
   Count    : 5
   Capacity : 5
   Values:
    -KEY-    -VALUE-
    five:    jumped
    four:    fox
    one:    The
    three:    brown
    two:    quick

After Clear,
   Count    : 0
   Capacity : 16
   Values:
    -KEY-    -VALUE-

After the second TrimToSize,
   Count    : 0
   Capacity : 16
   Values:
    -KEY-    -VALUE-
*/ 
#using <system.dll>

using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( SortedList^ myList )
{
   Console::WriteLine(  "\t-KEY-\t-VALUE-" );
   for ( int i = 0; i < myList->Count;
 i++ )
   {
      Console::WriteLine(  "\t{0}:\t{1}", myList->GetKey( i ), myList->GetByIndex(
 i ) );

   }
   Console::WriteLine();
}

int main()
{
   
   // Creates and initializes a new SortedList.
   SortedList^ mySL = gcnew SortedList;
   mySL->Add( "one", "The" );
   mySL->Add( "two", "quick" );
   mySL->Add( "three", "brown" );
   mySL->Add( "four", "fox" );
   mySL->Add( "five", "jumped" );
   
   // Displays the count, capacity and values of the SortedList.
   Console::WriteLine(  "Initially," );
   Console::WriteLine(  "   Count    : {0}", mySL->Count );
   Console::WriteLine(  "   Capacity : {0}", mySL->Capacity );
   Console::WriteLine(  "   Values:" );
   PrintKeysAndValues( mySL );
   
   // Trims the SortedList.
   mySL->TrimToSize();
   
   // Displays the count, capacity and values of the SortedList.
   Console::WriteLine(  "After TrimToSize," );
   Console::WriteLine(  "   Count    : {0}", mySL->Count );
   Console::WriteLine(  "   Capacity : {0}", mySL->Capacity );
   Console::WriteLine(  "   Values:" );
   PrintKeysAndValues( mySL );
   
   // Clears the SortedList.
   mySL->Clear();
   
   // Displays the count, capacity and values of the SortedList.
   Console::WriteLine(  "After Clear," );
   Console::WriteLine(  "   Count    : {0}", mySL->Count );
   Console::WriteLine(  "   Capacity : {0}", mySL->Capacity );
   Console::WriteLine(  "   Values:" );
   PrintKeysAndValues( mySL );
   
   // Trims the SortedList again.
   mySL->TrimToSize();
   
   // Displays the count, capacity and values of the SortedList.
   Console::WriteLine(  "After the second TrimToSize," );
   Console::WriteLine(  "   Count    : {0}", mySL->Count );
   Console::WriteLine(  "   Capacity : {0}", mySL->Capacity );
   Console::WriteLine(  "   Values:" );
   PrintKeysAndValues( mySL );
}

/* 
This code produces the following output.

Initially,
   Count    : 5
   Capacity : 16
   Values:
        -KEY-   -VALUE-
        five:   jumped
        four:   fox
        one:    The
        three:  brown
        two:    quick

After TrimToSize,
   Count    : 5
   Capacity : 5
   Values:
        -KEY-   -VALUE-
        five:   jumped
        four:   fox
        one:    The
        three:  brown
        two:    quick

After Clear,
   Count    : 0
   Capacity : 16
   Values:
        -KEY-   -VALUE-

After the second TrimToSize,
   Count    : 0
   Capacity : 16
   Values:
        -KEY-   -VALUE-

*/
import System.*;
import System.Collections.*;

public class SamplesSortedList
{
    public static void main(String[]
 args)
    {
        // Creates and initializes a new SortedList.
        SortedList mySL = new SortedList();

        mySL.Add("one", "The");
        mySL.Add("two", "quick");
        mySL.Add("three", "brown");
        mySL.Add("four", "fox");
        mySL.Add("five", "jumped");

        // Displays the count, capacity and values of the SortedList.
        Console.WriteLine("Initially,");
        Console.WriteLine("   Count    : {0}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_Capacity()));
        Console.WriteLine("   Values:");
        PrintKeysAndValues(mySL);

        // Trims the SortedList.
        mySL.TrimToSize();

        // Displays the count, capacity and values of the SortedList.
        Console.WriteLine("After TrimToSize,");
        Console.WriteLine("   Count    : {0}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_Capacity()));
        Console.WriteLine("   Values:");
        PrintKeysAndValues(mySL);

        // Clears the SortedList.
        mySL.Clear();

        // Displays the count, capacity and values of the SortedList.
        Console.WriteLine("After Clear,");
        Console.WriteLine("   Count    : {0}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_Capacity()));
        Console.WriteLine("   Values:");
        PrintKeysAndValues(mySL);

        // Trims the SortedList again.
        mySL.TrimToSize();

        // Displays the count, capacity and values of the SortedList.
        Console.WriteLine("After the second TrimToSize,");
        Console.WriteLine("   Count    : {0}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_Capacity()));
        Console.WriteLine("   Values:");
        PrintKeysAndValues(mySL);
    } //main

    public static void PrintKeysAndValues(SortedList
 myList)
    {
        Console.WriteLine("\t-KEY-\t-VALUE-");
        for (int i = 0; i < myList.get_Count();
 i++) {
            Console.WriteLine("\t{0}:\t{1}", myList.GetKey(i), 
                myList.GetByIndex(i));
        }
        Console.WriteLine();
    } //PrintKeysAndValues

} //SamplesSortedList

/* 
 This code produces the following output.
 
 Initially,
    Count    : 5
    Capacity : 16
    Values:
     -KEY-    -VALUE-
     five:    jumped
     four:    fox
     one:    The
     three:    brown
     two:    quick
 
 After TrimToSize,
    Count    : 5
    Capacity : 5
    Values:
     -KEY-    -VALUE-
     five:    jumped
     four:    fox
     one:    The
     three:    brown
     two:    quick
 
 After Clear,
    Count    : 0
    Capacity : 16
    Values:
     -KEY-    -VALUE-
 
 After the second TrimToSize,
    Count    : 0
    Capacity : 16
    Values:
     -KEY-    -VALUE-
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


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

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

辞書ショートカット

すべての辞書の索引

「SortedList.Clear」の関連用語

SortedList.Clearのお隣キーワード
検索ランキング

   

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



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

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

©2025 GRAS Group, Inc.RSS