ArrayList.SetRange(Int32, ICollection) Method is used to copy the elements of a collection over a range of elements in the ArrayList.
Syntax:
public virtual void SetRange (int index, System.Collections.ICollection c);
Parameters:
index: It is a zero-based ArrayList index at which to start copying the elements of c. The type of this parameter is System.Int32.
c: It is an ICollection whose elements to copy to the ArrayList. The collection itself cannot be null, but it can contain elements that are null.
Exceptions:
- ArgumentNullException: If the value of c is null.
- NotSupportedException: If the ArrayList is read-only.
- ArgumentOutOfRangeException: If the index is less than zero or [index + number of elements in c] > Count.
Below given are some examples to understand the implementation in a better way:
Example 1:
using System;
using System.Collections;
class GFG {
public static void Main()
{
ArrayList mylist = new ArrayList();
mylist.Add( "G" );
mylist.Add( "e" );
mylist.Add( "e" );
mylist.Add( "k" );
mylist.Add( "s" );
mylist.Add( "G" );
mylist.Add( "F" );
mylist.Add( "G" );
string [] str = { "This" , "is" , "C#" , "Tutorial" };
mylist.SetRange(0, str);
Show( "ArrayList is" , mylist);
}
static void Show( string arr, ArrayList mylist)
{
for ( int j = 0; j < mylist.Count; j++) {
Console.WriteLine(arr + "[" + j + "] = " + mylist[j]);
}
}
}
|
Output:
ArrayList is[0] = This
ArrayList is[1] = is
ArrayList is[2] = C#
ArrayList is[3] = Tutorial
ArrayList is[4] = s
ArrayList is[5] = G
ArrayList is[6] = F
ArrayList is[7] = G
Example 2:
using System;
using System.Collections;
class GFG {
public static void Main()
{
ArrayList mylist1 = new ArrayList();
mylist1.Add( "Hello " );
mylist1.Add( "Welcome " );
mylist1.Add( "to " );
mylist1.Add( "online " );
mylist1.Add( "portal " );
mylist1.Add( "of " );
mylist1.Add( "Geeks " );
mylist1.Add( "for " );
mylist1.Add( "Geeks " );
ArrayList mylist2 = new ArrayList();
mylist2.Add( "This " );
mylist2.Add( "is " );
mylist2.Add( "C# " );
mylist2.Add( "tutorial" );
mylist2.Add( "." );
ArrayList result = mylist1.GetRange(0, 6);
Console.WriteLine( "String from index number 0 to 6:" );
{
foreach (Object obj in result)
Console.Write( "{0}" , obj);
Console.WriteLine();
}
mylist1.SetRange(1, mylist2);
result = mylist1.GetRange(0, 6);
Console.WriteLine( "After SetRange() Method:" );
{
foreach (Object obj in result)
Console.Write( "{0}" , obj);
Console.WriteLine();
}
}
}
|
Output:
String from index number 0 to 6:
Hello Welcome to online portal of
After SetRange() Method:
Hello This is C# tutorial.
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Aug, 2019
Like Article
Save Article