📜  C#|从源ArrayList获取元素的子集

📅  最后修改于: 2021-05-29 18:50:27             🧑  作者: Mango

ArrayList.GetRange(Int32,Int32)方法用于获取一个ArrayList,它将表示源ArrayList中元素的子集。

句法:

public virtual System.Collections.ArrayList GetRange (int index, int count);

参数:

返回值:该方法返回一个ArrayList,它表示源ArrayList中元素的子集。

例外情况:

  • ArgumentOutOfRangeException:如果索引的值小于零,或者count的值小于零。
  • ArgumentException:如果索引和计数的值未表示ArrayList中元素的有效范围。

下面的程序说明了上面讨论的方法:

范例1:

// C# program to illustrate the
// concept of GetRange() Method
using System;
using System.Collections;
  
class GFG {
  
    // Main method
    public static void Main()
    {
  
        // Creates and initializes
        // a new ArrayList.
        ArrayList myarraylist = new ArrayList();
        myarraylist.Add("Welcome");
        myarraylist.Add("to");
        myarraylist.Add("Geeks");
        myarraylist.Add("for");
        myarraylist.Add("Geeks");
        myarraylist.Add("portal");
  
        // Creates and initializes queue
        Queue mynewList = new Queue();
        mynewList.Enqueue("This");
        mynewList.Enqueue("is");
        mynewList.Enqueue("C#");
        mynewList.Enqueue("tutorial");
  
        // Displays the values of six 
        // elements starting at index 0.
        ArrayList newarraylist = myarraylist.GetRange(0, 6);
        Console.WriteLine("Elements are:");
        Displaydata(newarraylist, '\n');
  
        // Replaces the values of six elements 
        // starting at index 1 with the values
        // in the queue.
        myarraylist.SetRange(2, mynewList);
  
        // Displays the values of six 
        // elements starting at index 0.
        newarraylist = myarraylist.GetRange(0, 6);
        Console.WriteLine("\nNow elements are:");
        Displaydata(newarraylist, '\n');
    }
  
    public static void Displaydata(IEnumerable myvalueList,
                                          char mySeparator)
    {
        foreach(Object obj in myvalueList)
            Console.Write("{0}{1}", mySeparator, obj);
        Console.WriteLine();
    }
}

输出:

Elements are:

Welcome
to
Geeks
for
Geeks
portal

Now elements are:

Welcome
to
This
is
C#
tutorial

范例2:

// C# program to illustrate the
// concept of GetRange() Method
using System;
using System.Collections;
  
class GFG {
  
    // Main method
    public static void Main()
    {
  
        // Creates and initializes a new ArrayList.
        ArrayList myarraylist = new ArrayList();
        myarraylist.Add("Welcome");
        myarraylist.Add("to");
        myarraylist.Add("Geeks");
        myarraylist.Add("for");
        myarraylist.Add("Geeks");
        myarraylist.Add("portal");
  
        // Creates and initializes queue
        Queue mynewList = new Queue();
        mynewList.Enqueue("This");
        mynewList.Enqueue("is");
        mynewList.Enqueue("C#");
        mynewList.Enqueue("tutorial");
  
        // Displays the values of six elements
        ArrayList newarraylist = myarraylist.GetRange(-1, 6);
        Console.WriteLine("Elements are:");
        Displaydata(newarraylist, '\n');
  
        // Replaces the values of six elements 
        // starting at index 1 with the 
        // values in the queue.
        myarraylist.SetRange(2, mynewList);
  
        // Displays the values of six 
        // elements starting at index 0.
        newarraylist = myarraylist.GetRange(0, 6);
        Console.WriteLine("Now elements are:");
        Displaydata(newarraylist, '\n');
    }
  
    public static void Displaydata(IEnumerable myvalueList, 
                                          char mySeparator)
    {
        foreach(Object obj in myvalueList)
            Console.Write("{0}{1}", mySeparator, obj);
        Console.WriteLine();
    }
}

运行时错误:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.arraylist.getrange?view=netframework-4.7.2#System_Collections_ArrayList_GetRange_System_Int32_System_Int32_