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

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ArrayList.InsertRange メソッドの意味・解説 

ArrayList.InsertRange メソッド

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

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

Public Overridable Sub InsertRange
 ( _
    index As Integer, _
    c As ICollection _
)
Dim instance As ArrayList
Dim index As Integer
Dim c As ICollection

instance.InsertRange(index, c)
public virtual void InsertRange (
    int index,
    ICollection c
)
public:
virtual void InsertRange (
    int index, 
    ICollection^ c
)
public void InsertRange (
    int index, 
    ICollection c
)
public function InsertRange (
    index : int, 
    c : ICollection
)

パラメータ

index

新し要素挿入される位置の 0 から始まるインデックス

c

ArrayList に要素挿入する ICollection。コレクション自体null 参照 (Visual Basic では Nothing) にすることはできませんが、コレクション格納する要素null 参照 (Visual Basic では Nothing) であってもかまいません

例外例外
例外種類条件

ArgumentNullException

cnull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

index が 0 未満です。

または

indexCount より大きい

NotSupportedException

ArrayList読み取り専用です。

または

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 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.
' 
' 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()
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 )  {
      foreach ( Object obj in myList )
         Console.Write( "   {0}", obj );
      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()
*/

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 = gcnew 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 = gcnew 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: {0}", myException );
   }

}

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   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()
 */
import System.*;
import System.Collections.*;

public class SamplesArrayList
{
    public static void main(String[]
 args)
    {
        // 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.get_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.get_Count() + 1, "anystring");
        }
        catch (System.Exception myException) {
            Console.WriteLine("Exception: " + myException.ToString());
        }
    } //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.
 
 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(String[] args)
 */
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()
 */ 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ArrayList クラス
ArrayList メンバ
System.Collections 名前空間
Insert
AddRange
SetRange
GetRange
RemoveRange


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

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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2024 GRAS Group, Inc.RSS