ArrayList.GetRange 方法

返回 ArrayList,它表示源 ArrayList 中元素的子集。

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

语法

声明
Public Overridable Function GetRange ( _
    index As Integer, _
    count As Integer _
) As ArrayList
用法
Dim instance As ArrayList
Dim index As Integer
Dim count As Integer
Dim returnValue As ArrayList

returnValue = instance.GetRange(index, count)
public virtual ArrayList GetRange (
    int index,
    int count
)
public:
virtual ArrayList^ GetRange (
    int index, 
    int count
)
public ArrayList GetRange (
    int index, 
    int count
)
public function GetRange (
    index : int, 
    count : int
) : ArrayList

参数

  • index
    范围开始处的从零开始的 ArrayList 索引。
  • count
    范围中的元素数。

返回值

ArrayList,它表示源 ArrayList 中元素的子集。

异常

异常类型 条件

ArgumentOutOfRangeException

index 小于零。

- 或 -

count 小于零。

ArgumentException

index 和 count 不表示 ArrayList 中元素的有效范围。

备注

此方法不创建元素的副本。新 ArrayList 只是一个进入源 ArrayList 的视图窗口。但是,随后对源 ArrayList 进行的所有更改必须通过此视图窗口 ArrayList 完成。如果直接对源 ArrayList 进行更改,则该视图窗口 ArrayList 将无效,对其执行的任何操作将返回 InvalidOperationException

此方法的运算复杂度为 O(1)。

示例

下面的代码示例演示如何设置和获取 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 命名空间
RemoveRange
AddRange
InsertRange
SetRange