C# ArrayList GetRange
Description
ArrayList GetRange
returns an ArrayList which represents
a subset of the elements in the source ArrayList.
Syntax
ArrayList.GetRange
has the following syntax.
public virtual ArrayList GetRange(
int index,
int count
)
Parameters
ArrayList.GetRange
has the following parameters.
index
- The zero-based ArrayList index at which the range starts.count
- The number of elements in the range.
Returns
ArrayList.GetRange
method returns An ArrayList which represents a subset of the elements in the source ArrayList.
Example
The following code
uses the GetRange()
method to get
two elements from myArrayList
, starting at index 1
using System;/*from w w w . j a v a 2 s . c o m*/
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here's", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
ArrayList anotherArrayList = myArrayList.GetRange(1, 2);
DisplayArrayList("anotherArrayList", anotherArrayList);
}
public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
{
for (int i = 0; i < myArrayList.Count; i++){
Console.WriteLine(arrayListName + "[" + i + "] = " +
myArrayList[i]);
}
}
}
The code above generates the following result.