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)인 요소를 포함할 수는 있습니다.
예외
예외 형식 | 조건 |
---|---|
c가 Null 참조(Visual Basic의 경우 Nothing)인 경우 |
|
index가 0보다 작은 경우 - 또는 - index는 Count보다 큽니다. |
|
ArrayList가 읽기 전용인 경우 - 또는 - ArrayList의 크기가 고정되어 있는 경우 |
설명
ArrayList은 Null 참조(Visual Basic의 경우 Nothing)을 유효한 값으로 받아들이며 중복 요소를 허용합니다.
새 Count(현재 Count에 컬렉션의 크기를 더한 값)가 Capacity보다 크면 ArrayList의 용량이 새 요소를 수용하기 위해 내부 배열을 자동으로 재할당하여 증가하며 기존 요소가 새 배열로 복사된 후 새 요소가 추가됩니다.
index가 Count와 같으면 ArrayList의 끝에 요소가 추가됩니다.
ICollection의 요소 순서는 ArrayList에서 유지됩니다.
목록과 같은 연속 요소 컬렉션에서 삽입 지점 다음에 오는 요소는 새 요소를 수용할 수 있도록 아래로 이동합니다. 컬렉션이 인덱싱되면 이동되는 요소의 인덱스도 업데이트됩니다. 이 동작은 개념적으로 요소가 해시 테이블과 같은 버킷으로 그룹화되는 컬렉션에는 적용되지 않습니다.
이 메서드는 O(n + m) 연산이며, 여기서 n은 추가할 요소 수이고 m은 Count입니다.
예제
다음 코드 예제에서는 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에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.
버전 정보
.NET Framework
2.0, 1.1, 1.0에서 지원
.NET Compact Framework
2.0, 1.0에서 지원
참고 항목
참조
ArrayList 클래스
ArrayList 멤버
System.Collections 네임스페이스
Insert
AddRange
SetRange
GetRange
RemoveRange