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:
Similar Reads
- C# | Remove a range of elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.RemoveRange(Int32, Int32) method is used to remove a range of ele 3 min read
- C# | Remove all elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Clear method is used to remove all the elements from the ArrayLis 3 min read
- C# | Adding the elements to the end of the ArrayList ArrayList.AddRange(ICollection) Method is used to add the elements of an ICollection to the end of the ArrayList. Syntax: public virtual void AddRange (System.Collections.ICollection c); Here, c is the ICollection whose elements should be added to the end of the ArrayList. The collection itself cann 2 min read
- C# | Getting an enumerator for a range of elements in the ArrayList ArrayList.GetEnumerator(Int32, Int32) method is used to get an enumerator for a range of elements in the ArrayList. Syntax: public virtual System.Collections.IEnumerator GetEnumerator (int index, int count); Parameters: index: It is the zero-based starting index of type Int32 of the ArrayList sectio 3 min read
- C# | Adding elements to the end of the ArrayList AddRange(ICollection) Method is used to add the elements of an ICollection to the end of the ArrayList. Or in other words, this method is used to add the multiple elements from other collection into an ArrayList. Here elements are defined as the primitive or non-primitive type. Syntax: public virtua 3 min read
- C# | Get or set the number of elements that the ArrayList can contain ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Capacity property is used to get or set the number of elements th 2 min read
- C# | Remove the element at the specified index of the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.RemoveAt(Int32) method is used to remove the element at the speci 3 min read
- C# | Insert an element into the ArrayList at the specified index ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayLis 3 min read
- C# | Get the number of elements actually contained in the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Count property gets the number of elements actually contained in 3 min read
- C# | Remove the first occurrence of a specific object from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Remove(Object) method is used to remove the first occurrence of a 3 min read