ArrayList.SetRange メソッド
コレクションの要素を ArrayList 内の要素の範囲にコピーします。
Public Overridable Sub SetRange( _
ByVal index As Integer, _ ByVal c As ICollection _)
[C#]
public virtual void SetRange(intindex,ICollectionc);
[C++]
public: virtual void SetRange(intindex,ICollection* c);
[JScript]
public function SetRange(
index : int,c : ICollection);
パラメータ
- index
c の要素のコピーを開始する位置を示す、0 から始まる ArrayList インデックス。 - c
要素を ArrayList にコピーする ICollection 。コレクション自体を null 参照 (Visual Basic では Nothing) にすることはできませんが、コレクションに格納する要素は null 参照 (Nothing) であってもかまいません。
例外
例外の種類 | 条件 |
---|---|
ArgumentOutOfRangeException | index が 0 未満です。
または index と、 c 内の要素の数を合計した値が、 Count よりも大きい値です。 |
ArgumentNullException | c が null 参照 (Visual Basic では Nothing) です。 |
NotSupportedException | ArrayList が読み取り専用です。 |
解説
ArrayList は、 null 参照 (Visual Basic では Nothing) を有効な値として受け取り、要素の重複を許可します。
ICollection 内の要素の順序は、 ArrayList に保持されます。
使用例
ArrayList 内の要素の範囲を設定および取得する方法の例を次に示します。
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList()
myAL.Add("The")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumped")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")
' Creates and initializes the source ICollection.
Dim mySourceList As New Queue()
mySourceList.Enqueue("big")
mySourceList.Enqueue("gray")
mySourceList.Enqueue("wolf")
' Displays the values of five elements starting at index 0.
Dim mySubAL As ArrayList = myAL.GetRange(0, 5)
Console.WriteLine("Index 0 through 4 contains:")
PrintValues(mySubAL, ControlChars.Tab)
' Replaces the values of five elements starting at index 1 with the
' values in the ICollection.
myAL.SetRange(1, mySourceList)
' Displays the values of five elements starting at index 0.
mySubAL = myAL.GetRange(0, 5)
Console.WriteLine("Index 0 through 4 now contains:")
PrintValues(mySubAL, ControlChars.Tab)
End Sub
Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
Dim myEnumerator As System.Collections.IEnumerator = _
myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
End While
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' Index 0 through 4 contains:
' The quick brown fox jumped
' Index 0 through 4 now contains:
' The big gray wolf jumped
[C#]
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
// Creates and initializes the source ICollection.
Queue mySourceList = new Queue();
mySourceList.Enqueue( "big" );
mySourceList.Enqueue( "gray" );
mySourceList.Enqueue( "wolf" );
// Displays the values of five elements starting at index 0.
ArrayList mySubAL = myAL.GetRange( 0, 5 );
Console.WriteLine( "Index 0 through 4 contains:" );
PrintValues( mySubAL, '\t' );
// Replaces the values of five elements starting at index 1 with the values in the ICollection.
myAL.SetRange( 1, mySourceList );
// Displays the values of five elements starting at index 0.
mySubAL = myAL.GetRange( 0, 5 );
Console.WriteLine( "Index 0 through 4 now contains:" );
PrintValues( mySubAL, '\t' );
}
public static void PrintValues( IEnumerable myList, char mySeparator ) {
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
Console.WriteLine();
}
}
/*
This code produces the following output.
Index 0 through 4 contains:
The quick brown fox jumped
Index 0 through 4 now contains:
The big gray wolf jumped
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable* myList, String* mySeparator );
int main() {
// Creates and initializes a new ArrayList.
ArrayList* myAL = new ArrayList();
myAL->Add( S"The" );
myAL->Add( S"quick" );
myAL->Add( S"brown" );
myAL->Add( S"fox" );
myAL->Add( S"jumped" );
myAL->Add( S"over" );
myAL->Add( S"the" );
myAL->Add( S"lazy" );
myAL->Add( S"dog" );
// Creates and initializes the source ICollection.
Queue* mySourceList = new Queue();
mySourceList->Enqueue( S"big" );
mySourceList->Enqueue( S"gray" );
mySourceList->Enqueue( S"wolf" );
String* szSeparator = "\t";
// Displays the values of five elements starting at index 0.
ArrayList* mySubAL = myAL->GetRange( 0, 5 );
Console::WriteLine( "Index 0 through 4 contains:" );
PrintValues( mySubAL, szSeparator );
// Replaces the values of five elements starting at index 1 with the values in the ICollection.
myAL->SetRange( 1, mySourceList );
// Displays the values of five elements starting at index 0.
mySubAL = myAL->GetRange( 0, 5 );
Console::WriteLine( "Index 0 through 4 now contains:" );
PrintValues( mySubAL, szSeparator );
}
void PrintValues( IEnumerable* myList, String* mySeparator ) {
System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
while ( myEnumerator->MoveNext() )
Console::Write( "{0}{1}", mySeparator, myEnumerator->Current );
Console::WriteLine();
}
/*
This code produces the following output.
Index 0 through 4 contains:
The quick brown fox jumped
Index 0 through 4 now contains:
The big gray wolf jumped
*/
[JScript]
import System;
import System.Collections;
// Creates and initializes a new ArrayList.
var myAL : ArrayList = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
// Creates and initializes the source ICollection.
var mySourceList : Queue = new Queue();
mySourceList.Enqueue( "big" );
mySourceList.Enqueue( "gray" );
mySourceList.Enqueue( "wolf" );
// Displays the values of five elements starting at index 0.
var mySubAL : ArrayList = myAL.GetRange( 0, 5 );
Console.WriteLine( "Index 0 through 4 contains:" );
PrintValues( mySubAL, '\t' );
// Replaces the values of five elements starting at index 1 with the values in the ICollection.
myAL.SetRange( 1, mySourceList );
// Displays the values of five elements starting at index 0.
mySubAL = myAL.GetRange( 0, 5 );
Console.WriteLine( "Index 0 through 4 now contains:" );
PrintValues( mySubAL, '\t' );
function PrintValues( myList : IEnumerable, mySeparator : char ) {
var myEnumerator : System.Collections.IEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
Console.WriteLine();
}
/*
This code produces the following output.
Index 0 through 4 contains:
The quick brown fox jumped
Index 0 through 4 now contains:
The big gray wolf jumped
*/
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard
参照
ArrayList クラス | ArrayList メンバ | System.Collections 名前空間 | AddRange | InsertRange | GetRange | RemoveRange