ArrayList.Insert メソッド
ArrayList 内の指定したインデックスの位置に要素を挿入します。
Public Overridable Sub Insert( _
ByVal index As Integer, _ ByVal value As Object _) Implements IList.Insert
[C#]
public virtual void Insert(intindex,objectvalue);
[C++]
public: virtual void Insert(intindex,Object* value);
[JScript]
public function Insert(
index : int,value : Object);
パラメータ
- index
value を挿入する位置の、0 から始まるインデックス番号。 - value
挿入する Object 。値は null 参照 (Visual Basic では Nothing) に設定できます。
実装
例外
例外の種類 | 条件 |
---|---|
ArgumentOutOfRangeException | index が 0 未満です。
または index が Count より大きい値です。 |
NotSupportedException | ArrayList が読み取り専用です。
または ArrayList が固定サイズです。 |
解説
ArrayList は、 null 参照 (Visual Basic では Nothing) を有効な値として受け取り、要素の重複を許可します。
Count が既に Capacity に等しい場合には、新しい要素を挿入する前に、内部配列を自動的に再割り当てすることによって、リストの容量が 2 倍になります。
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 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 名前空間 | InsertRange | Add | Remove