ArrayList.RemoveRange(Int32, Int32) メソッド

定義

ArrayList から要素の範囲を削除します。

public:
 virtual void RemoveRange(int index, int count);
public virtual void RemoveRange (int index, int count);
abstract member RemoveRange : int * int -> unit
override this.RemoveRange : int * int -> unit
Public Overridable Sub RemoveRange (index As Integer, count As Integer)

パラメーター

index
Int32

削除する要素の範囲の開始位置を示す 0 から始まるインデックス。

count
Int32

削除する要素の数を指定します。

例外

index が 0 未満です。

または

count が 0 未満です。

index および countArrayList において要素の有効な範囲を表していません。

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" );
   myAL->Add( "over" );
   myAL->Add( "the" );
   myAL->Add( "lazy" );
   myAL->Add( "dog" );
   
   // Displays the ArrayList.
   Console::WriteLine( "The ArrayList initially contains the following:" );
   PrintValues( myAL );
   
   // Removes the element containing "lazy".
   myAL->Remove( "lazy" );
   
   // Displays the current state of the ArrayList.
   Console::WriteLine( "After removing \"lazy\":" );
   PrintValues( myAL );
   
   // Removes the element at index 5.
   myAL->RemoveAt( 5 );
   
   // Displays the current state of the ArrayList.
   Console::WriteLine( "After removing the element at index 5:" );
   PrintValues( myAL );
   
   // Removes three elements starting at index 4.
   myAL->RemoveRange( 4, 3 );
   
   // Displays the current state of the ArrayList.
   Console::WriteLine( "After removing three elements starting at index 4:" );
   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.
 
 The ArrayList initially contains the following:
    The   quick   brown   fox   jumps   over   the   lazy   dog
 After removing "lazy":
    The   quick   brown   fox   jumps   over   the   dog
 After removing the element at index 5:
    The   quick   brown   fox   jumps   the   dog
 After removing three elements starting at index 4:
    The   quick   brown   fox
 */
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" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );

      // Displays the ArrayList.
      Console.WriteLine( "The ArrayList initially contains the following:" );
      PrintValues( myAL );

      // Removes the element containing "lazy".
      myAL.Remove( "lazy" );

      // Displays the current state of the ArrayList.
      Console.WriteLine( "After removing \"lazy\":" );
      PrintValues( myAL );

      // Removes the element at index 5.
      myAL.RemoveAt( 5 );

      // Displays the current state of the ArrayList.
      Console.WriteLine( "After removing the element at index 5:" );
      PrintValues( myAL );

      // Removes three elements starting at index 4.
      myAL.RemoveRange( 4, 3 );

      // Displays the current state of the ArrayList.
      Console.WriteLine( "After removing three elements starting at index 4:" );
      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.

The ArrayList initially contains the following:
   The   quick   brown   fox   jumps   over   the   lazy   dog
After removing "lazy":
   The   quick   brown   fox   jumps   over   the   dog
After removing the element at index 5:
   The   quick   brown   fox   jumps   the   dog
After removing three elements starting at index 4:
   The   quick   brown   fox
*/
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")
        myAL.Add("over")
        myAL.Add("the")
        myAL.Add("lazy")
        myAL.Add("dog")
        
        ' Displays the ArrayList.
        Console.WriteLine("The ArrayList initially contains the following:")
        PrintValues(myAL)
        
        ' Removes the element containing "lazy".
        myAL.Remove("lazy")
        
        ' Displays the current state of the ArrayList.
        Console.WriteLine("After removing ""lazy"":")
        PrintValues(myAL)
        
        ' Removes the element at index 5.
        myAL.RemoveAt(5)
        
        ' Displays the current state of the ArrayList.
        Console.WriteLine("After removing the element at index 5:")
        PrintValues(myAL)
        
        ' Removes three elements starting at index 4.
        myAL.RemoveRange(4, 3)
        
        ' Displays the current state of the ArrayList.
        Console.WriteLine("After removing three elements starting at index 4:")
        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.
' 
' The ArrayList initially contains the following:
'    The   quick   brown   fox   jumps   over   the   lazy   dog
' After removing "lazy":
'    The   quick   brown   fox   jumps   over   the   dog
' After removing the element at index 5:
'    The   quick   brown   fox   jumps   the   dog
' After removing three elements starting at index 4:
'    The   quick   brown   fox

注釈

リストなどの連続する要素のコレクションでは、空白になった位置を埋めるために、削除された要素の後にある要素の位置が繰り上げられます。 コレクションにインデックスがある場合、移動した要素のインデックスも更新されます。 この動作は、要素が概念的にバケットにグループ化されているハッシュ テーブルなどのコレクションには適用されません。

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

適用対象

こちらもご覧ください