C# | Copy the elements of collection over a range of elements in ArrayList (original) (raw)
Last Updated : 28 Aug, 2019
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:
CSharp `
// C# Program to illustrate the // SetRange() Method using System; using System.Collections;
class GFG {
// Main method
public static void Main()
{
// Create and initialize ArrayList
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");
// There are total 4 elements
string[] str = { "This", "is", "C#", "Tutorial" };
// using SetRange() Method
// starting from index 0
mylist.SetRange(0, str);
Show("ArrayList is", mylist);
}
// show method to display the result
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:
CSharp `
// C# Program to illustrate the // SetRange() Method using System; using System.Collections;
class GFG { // Main method public static void Main() { // Create and initialize ArrayList 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 ");
// Create and initialize new ArrayList
ArrayList mylist2 = new ArrayList();
mylist2.Add("This ");
mylist2.Add("is ");
mylist2.Add("C# ");
mylist2.Add("tutorial");
mylist2.Add(".");
// Displays the values of 6
// elements of mylist1 starting
// at index 0.
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();
}
// Replace the value of 5 element
// starting from index 1
// with the values in mylist2
mylist1.SetRange(1, mylist2);
// Display the result
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:
Similar Reads
- C# | Copying the Collection elements to an array Collection.CopyTo(T[], Int32) method is used to copy the entire Collection to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public void CopyTo (T[] array, int index); Parameters: array : The one-dimensional Array that is the destin 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# | 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# | 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 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# | Getting a subset of the elements from the source ArrayList 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 ArrayLi 3 min read
- C# | ArrayList whose elements are copies of the specified value ArrayList.Repeat(Object, Int32) Method is used to return an ArrayList whose elements are copies of the specified value. Or in other words, this method is used when you want to repeat a specified element in the ArrayList. This method is an O(n) operation, where n is the number of times item should be 2 min read
- C# | Remove element at specified index of Collection Collection.RemoveAt(Int32) is used to remove the element at the specified index of the Collection. Syntax: public void RemoveAt (int index); Here, index is the zero-based index of the element to remove. Exception: This method will give ArgumentOutOfRangeException if the index is le 2 min read
- C# | Copying the elements of ArrayList to a new array ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows: ToArray()ToArray(Type)ToArray() This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Arra 2 min read
- C# | How to insert the elements of a collection into the List at the specified index List.InsertRange(Int32, IEnumerable) Method is used to insert the elements of a collection into the List at the specified index. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid 3 min read