C# | Getting a subset of the elements from the source ArrayList (original) (raw)
Last Updated : 01 Feb, 2019
ArrayList.GetRange(Int32, Int32) Method is used to get an ArrayList which will represent a subset of the elements in the source ArrayList.Syntax:
public virtual System.Collections.ArrayList GetRange (int index, int count);
Parameters:
index: It is of Int32 type and represents the zero-based ArrayList index at which the range starts.count: It is of Int32 type and represents the number of elements in the range.
Return Value: This method returns an ArrayList which represents a subset of the elements in the source ArrayList.Exceptions:
- ArgumentOutOfRangeException: If the value of the index is less than zero, or if the value of count is less than zero.
- ArgumentException: If the value of an index and count does not denote a valid range of elements in the ArrayList.
Below programs illustrate the above-discussed method:Example 1:
CSharp `
// 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();
}
}
`
Output:
Elements are:
Welcome to Geeks for Geeks portal
Now elements are:
Welcome to This is C# tutorial
Example 2:
CSharp `
// 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();
}
}
`
Runtime Error:
Unhandled Exception: System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: index
Reference: