ArrayList.Insert メソッド
アセンブリ: mscorlib (mscorlib.dll 内)



ArrayList は、null 参照 (Visual Basic では Nothing) を有効な値として受け取り、要素の重複を許可します。
Count が既に Capacity に等しい場合には、内部配列を自動的に再割り当てすることにより ArrayList の容量が増加し、新しい要素を追加する前に既存の要素は新しい配列にコピーされます。
index と Count が等しい場合は、value が 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() */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からArrayList.Insert メソッドを検索する場合は、下記のリンクをクリックしてください。

- ArrayList.Insert メソッドのページへのリンク