ArrayList.SetRange 方法

将集合中的元素复制到 ArrayList 中一定范围的元素上。

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable Sub SetRange ( _
    index As Integer, _
    c As ICollection _
)
用法
Dim instance As ArrayList
Dim index As Integer
Dim c As ICollection

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

参数

  • index
    从零开始的 ArrayList 索引,从该位置开始复制 c 的元素。
  • c
    ICollection,要将其元素复制到 ArrayList 中。集合本身不能为 空引用(在 Visual Basic 中为 Nothing),但它可以包含为 空引用(在 Visual Basic 中为 Nothing) 的元素。

异常

异常类型 条件

ArgumentOutOfRangeException

index 小于零。

- 或 -

index 加上 c 中的元素数大于 Count

ArgumentNullException

c 为 空引用(在 Visual Basic 中为 Nothing)。

NotSupportedException

ArrayList 为只读。

备注

ArrayList 接受 空引用(在 Visual Basic 中为 Nothing) 作为有效值并且允许重复的元素。

ICollection 中元素的顺序保存在 ArrayList 中。

此方法的运算复杂度为 O(n+ 1),其中 n 是 Count

示例

下面的代码示例演示如何设置和获取 ArrayList 中元素的范围。

Imports System
Imports System.Collections

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, vbTab)

        ' 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, vbTab)

    End Sub 'Main

    Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
        Dim obj As [Object]
        For Each obj In  myList
            Console.Write("{0}{1}", mySeparator, obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

End Class 'SamplesArrayList 


' 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
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 )  {
      foreach ( Object obj in myList )
         Console.Write( "{0}{1}", mySeparator, obj );
      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
*/ 
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList, char mySeparator );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew 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 = gcnew 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' );
}

void PrintValues( IEnumerable^ myList, char mySeparator )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "{0}{1}", mySeparator, obj );
   }

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

public class SamplesArrayList
{
    public static void main(String[] args)
    {
        // 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');
    } //main

    public static void PrintValues(IEnumerable myList, char mySeparator)
    {
        IEnumerator objMyEnum = myList.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            Console.Write("{0}{1}", (Char)mySeparator, obj);
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArrayList 

/* 
 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
 */
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 2000 SP4、Windows Millennium Edition、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

请参见

参考

ArrayList 类
ArrayList 成员
System.Collections 命名空间
AddRange
InsertRange
GetRange
RemoveRange