次の方法で共有


ArrayList.InsertRange メソッド

コレクションの要素を ArrayList 内の指定したインデックスの位置に挿入します。

Public Overridable Sub InsertRange( _
   ByVal index As Integer, _   ByVal c As ICollection _)
[C#]
public virtual void InsertRange(intindex,ICollectionc);
[C++]
public: virtual void InsertRange(intindex,ICollection* c);
[JScript]
public function InsertRange(
   index : int,c : ICollection);

パラメータ

  • index
    新しい要素が挿入される位置の 0 から始まるインデックス。
  • c
    ArrayList に要素を挿入する ICollection 。コレクション自体を null 参照 (Visual Basic では Nothing) にすることはできませんが、コレクションに格納する要素は null 参照 (Nothing) であってもかまいません。

例外

例外の種類 条件
ArgumentNullException c が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException index が 0 未満です。

または

index が Count より大きい値です。

NotSupportedException ArrayList が読み取り専用です。

または

ArrayList が固定サイズです。

解説

ArrayList は、 null 参照 (Visual Basic では Nothing) を有効な値として受け取り、要素の重複を許可します。

新しい Count (現在の Count とコレクションのサイズの合計) が Capacity より大きい場合、リストの容量は、元の容量の 2 倍かまたは新しいカウントのうち、いずれか大きい方になります。内部配列は、新しい要素を挿入するよう自動的に再割り当てされます。

index と Count が等しい場合、要素が ArrayList の末尾に追加されます。

ICollection 内の要素の順序は、 ArrayList に保持されます。

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

使用例

ArrayList に要素を追加する方法の例を次に示します。

 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new ArrayList using Insert instead of Add.
        Dim myAL As New ArrayList()
        myAL.Insert(0, "The")
        myAL.Insert(1, "fox")
        myAL.Insert(2, "jumps")
        myAL.Insert(3, "over")
        myAL.Insert(4, "the")
        myAL.Insert(5, "dog")
        
        ' Creates and initializes a new Queue.
        Dim myQueue As New Queue()
        myQueue.Enqueue("quick")
        myQueue.Enqueue("brown")
        
        ' Displays the ArrayList and the Queue.
        Console.WriteLine("The ArrayList initially contains the following:")
        PrintValues(myAL)
        Console.WriteLine("The Queue initially contains the following:")
        PrintValues(myQueue)
        
        ' Copies the Queue elements to the ArrayList at index 1.
        myAL.InsertRange(1, myQueue)
        
        ' Displays the ArrayList.
        Console.WriteLine("After adding the Queue, the ArrayList now contains:")
        PrintValues(myAL)
        
        ' Search for "dog" and add "lazy" before it.
        myAL.Insert(myAL.IndexOf("dog"), "lazy")
        
        ' Displays the ArrayList.
        Console.WriteLine("After adding ""lazy"", the ArrayList now contains:")
        PrintValues(myAL)
        
        ' Add "!!!" at the end.
        myAL.Insert(myAL.Count, "!!!")
        
        ' Displays the ArrayList.
        Console.WriteLine("After adding ""!!!"", the ArrayList now contains:")
        PrintValues(myAL)
        
        ' Inserting an element beyond Count throws an exception.
        Try
            myAL.Insert(myAL.Count + 1, "anystring")
        Catch myException As Exception
            Console.WriteLine("Exception: " + myException.ToString())
        End Try
    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.
' 
' The ArrayList initially contains the following:
'     The    fox    jumps    over    the    dog
' The Queue initially contains the following:
'     quick    brown
' After adding the Queue, the ArrayList now contains:
'     The    quick    brown    fox    jumps    over    the    dog
' After adding "lazy", the ArrayList now contains:
'     The    quick    brown    fox    jumps    over    the    lazy    dog
' After adding "!!!", the ArrayList now contains:
'     The    quick    brown    fox    jumps    over    the    lazy    dog    !!!
' Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
' Parameter name: index
'    at System.Collections.ArrayList.Insert(Int32 index, Object value)
'    at SamplesArrayList.Main()

[C#] 
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList using Insert instead of Add.
      ArrayList myAL = new ArrayList();
      myAL.Insert( 0, "The" );
      myAL.Insert( 1, "fox" );
      myAL.Insert( 2, "jumps" );
      myAL.Insert( 3, "over" );
      myAL.Insert( 4, "the" );
      myAL.Insert( 5, "dog" );

      // Creates and initializes a new Queue.
      Queue myQueue = new Queue();
      myQueue.Enqueue( "quick" );
      myQueue.Enqueue( "brown" );

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

      // Copies the Queue elements to the ArrayList at index 1.
      myAL.InsertRange( 1, myQueue );

      // Displays the ArrayList.
      Console.WriteLine( "After adding the Queue, the ArrayList now contains:" );
      PrintValues( myAL );

      // Search for "dog" and add "lazy" before it.
      myAL.Insert( myAL.IndexOf( "dog" ), "lazy" );

      // Displays the ArrayList.
      Console.WriteLine( "After adding \"lazy\", the ArrayList now contains:" );
      PrintValues( myAL );

      // Add "!!!" at the end.
      myAL.Insert( myAL.Count, "!!!" );

      // Displays the ArrayList.
      Console.WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
      PrintValues( myAL );

      // Inserting an element beyond Count throws an exception.
      try  {
         myAL.Insert( myAL.Count+1, "anystring" );
      } catch ( Exception myException )  {
         Console.WriteLine("Exception: " + myException.ToString());
      }
   }

   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.

The ArrayList initially contains the following:
        The     fox     jumps  over    the     dog
The Queue initially contains the following:
        quick   brown
After adding the Queue, the ArrayList now contains:
        The     quick   brown   fox     jumps  over    the     dog
After adding "lazy", the ArrayList now contains:
        The     quick   brown   fox     jumps  over    the     lazy    dog
After adding "!!!", the ArrayList now contains:
        The     quick   brown   fox     jumps  over    the     lazy    dog     !!!
Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
Parameter name: index
   at System.Collections.ArrayList.Insert(Int32 index, Object value)
   at SamplesArrayList.Main()
*/ 

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

void PrintValues( IEnumerable* myList );
 
int main()  {
 
       // Creates and initializes a new ArrayList using Insert instead of Add.
       ArrayList* myAL = new ArrayList();
       myAL->Insert( 0, S"The" );
       myAL->Insert( 1, S"fox" );
       myAL->Insert( 2, S"jumped" );
       myAL->Insert( 3, S"over" );
       myAL->Insert( 4, S"the" );
       myAL->Insert( 5, S"dog" );
 
       // Creates and initializes a new Queue.
       Queue* myQueue = new Queue();
       myQueue->Enqueue( S"quick" );
       myQueue->Enqueue( S"brown" );
 
       // Displays the ArrayList and the Queue.
       Console::WriteLine( "The ArrayList initially contains the following:" );
       PrintValues( myAL );
       Console::WriteLine( "The Queue initially contains the following:" );
       PrintValues( myQueue );
 
       // Copies the Queue elements to the ArrayList at index 1.
       myAL->InsertRange( 1, myQueue );
 
       // Displays the ArrayList.
       Console::WriteLine( "After adding the Queue, the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Searches for "dog" and add "lazy" before it.
       myAL->Insert( myAL->IndexOf( S"dog" ), S"lazy" );
 
       // Displays the ArrayList.
       Console::WriteLine( "After adding \"lazy\", the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Adds "!!!" at the end.
       myAL->Insert( myAL->Count, S"!!!" );
 
       // Displays the ArrayList.
       Console::WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Inserting an element beyond Count throws an exception.
       try  {
          myAL->Insert( myAL->Count+1, S"anystring" );
       } catch ( Exception* myException )  {
          Console::WriteLine(String::Concat("Exception: ", myException->ToString()));
       }
    }
 
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.
 
 The ArrayList initially contains the following:
         The     fox     jumped  over    the     dog
 The Queue initially contains the following:
         quick   brown
 After adding the Queue, the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     dog
 After adding "lazy", the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     lazy    dog
 After adding "!!!", the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     lazy    dog     !!!
 Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
 Parameter name: index
    at System.Collections.ArrayList.Insert(Int32 index, Object value)
    at SamplesArrayList.Main()
 */ 

[JScript] 
import System;
import System.Collections;


// Creates and initializes a new ArrayList using Insert instead of Add.
var myAL : ArrayList = new ArrayList();
myAL.Insert( 0, "The" );
myAL.Insert( 1, "fox" );
myAL.Insert( 2, "jumped" );
myAL.Insert( 3, "over" );
myAL.Insert( 4, "the" );
myAL.Insert( 5, "dog" );

// Creates and initializes a new Queue.
var myQueue : Queue = new Queue();
myQueue.Enqueue( "quick" );
myQueue.Enqueue( "brown" );

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

// Copies the Queue elements to the ArrayList at index 1.
myAL.InsertRange( 1, myQueue );

// Displays the ArrayList.
Console.WriteLine( "After adding the Queue, the ArrayList now contains:" );
PrintValues( myAL );

// Search for "dog" and add "lazy" before it.
myAL.Insert( myAL.IndexOf( "dog" ), "lazy" );

// Displays the ArrayList.
Console.WriteLine( "After adding \"lazy\", the ArrayList now contains:" );
PrintValues( myAL );

// Add "!!!" at the end.
myAL.Insert( myAL.Count, "!!!" );

// Displays the ArrayList.
Console.WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
PrintValues( myAL );

// Inserting an element beyond Count throws an exception.
try  {
  myAL.Insert( myAL.Count+1, "anystring" );
} catch ( myException : Exception )  {
  Console.WriteLine("Exception: " + myException.ToString());
}


 
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.
 
 The ArrayList initially contains the following:
     The    fox    jumped    over    the    dog
 The Queue initially contains the following:
     quick    brown
 After adding the Queue, the ArrayList now contains:
     The    quick    brown    fox    jumped    over    the    dog
 After adding "lazy", the ArrayList now contains:
     The    quick    brown    fox    jumped    over    the    lazy    dog
 After adding "!!!", the ArrayList now contains:
     The    quick    brown    fox    jumped    over    the    lazy    dog    !!!
 Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
 Parameter name: index
    at System.Collections.ArrayList.Insert(Int32 index, Object value)
    at JScript 0.Global Code()
 */ 

必要条件

プラットフォーム: 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 名前空間 | Insert | AddRange | SetRange | GetRange | RemoveRange