ArrayList.SetRange(Int32, ICollection) 方法

定义

复制 ArrayList 中一个子集合的元素。

public:
 virtual void SetRange(int index, System::Collections::ICollection ^ c);
public virtual void SetRange (int index, System.Collections.ICollection c);
abstract member SetRange : int * System.Collections.ICollection -> unit
override this.SetRange : int * System.Collections.ICollection -> unit
Public Overridable Sub SetRange (index As Integer, c As ICollection)

参数

index
Int32

从零开始的 ArrayList 索引,从此索引处开始复制 c 的元素。

c
ICollection

ICollection,它的元素要复制到 ArrayList。 集合本身不能为 null,但它可以包含为 null 的元素。

例外

index 小于零。

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

cnull

示例

下面的代码示例演示如何设置和获取其中 ArrayList一系列元素。

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( "jumps" );
   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     jumps
 Index 0 through 4 now contains:
         The     big     gray    wolf    jumps
 */
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( "jumps" );
      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     jumps
Index 0 through 4 now contains:
        The     big     gray    wolf    jumps
*/
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("jumps")
        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

    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

End Class


' This code produces the following output.
' 
' Index 0 through 4 contains:
'         The     quick   brown   fox     jumps
' Index 0 through 4 now contains:
'         The     big     gray    wolf    jumps

注解

ArrayList 接受 null 为有效值并允许重复元素。

ICollectionArrayList元素的顺序保留在 .

此方法是一个O(n)操作,其中 nCount

适用于

另请参阅