ArrayList.Repeat[Object, Int32] Method
- Reference
Please rate your experience
Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy.
Thank you.
Definition
Important
Some information relates to prerelease product that may be substantially modified before its released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
In this article
Returns an ArrayList whose elements are copies of the specified value.
Parameters
The Object to copy multiple times in the new ArrayList. The value can be null.
The number of times value should be copied.
Returns
ArrayListAn ArrayList with count number of elements, all of which are copies of value.
Exceptions
count is less than zero.
Examples
The following code example shows how to create and initialize a new ArrayList with the same value.
using namespace System; using namespace System::Collections; void PrintValues[ IEnumerable^ myList ]; int main[] { // Creates a new ArrayList with five elements and initialize each element with a null value. ArrayList^ myAL = ArrayList::Repeat[ 0, 5 ]; // Displays the count, capacity and values of the ArrayList. Console::WriteLine[ "ArrayList with five elements with a null value" ]; Console::WriteLine[ " Count : {0}", myAL->Count ]; Console::WriteLine[ " Capacity : {0}", myAL->Capacity ]; Console::Write[ " Values:" ]; PrintValues[ myAL ]; // Creates a new ArrayList with seven elements and initialize each element with the string "abc". myAL = ArrayList::Repeat[ "abc", 7 ]; // Displays the count, capacity and values of the ArrayList. Console::WriteLine[ "ArrayList with seven elements with a string value" ]; Console::WriteLine[ " Count : {0}", myAL->Count ]; Console::WriteLine[ " Capacity : {0}", myAL->Capacity ]; Console::Write[ " Values:" ]; PrintValues[ myAL ]; } void PrintValues[ IEnumerable^ myList ] { IEnumerator^ myEnum = myList->GetEnumerator[]; while [ myEnum->MoveNext[] ] { Object^ obj = safe_cast[myEnum->Current]; Console::Write[ " {0}", obj ]; } Console::WriteLine[]; } /* This code produces the following output. ArrayList with five elements with a null value Count : 5 Capacity : 16 Values: ArrayList with seven elements with a string value Count : 7 Capacity : 16 Values: abc abc abc abc abc abc abc */ using System; using System.Collections; public class SamplesArrayList { public static void Main[] { // Creates a new ArrayList with five elements and initialize each element with a null value. ArrayList myAL = ArrayList.Repeat[ null, 5 ]; // Displays the count, capacity and values of the ArrayList. Console.WriteLine[ "ArrayList with five elements with a null value" ]; Console.WriteLine[ " Count : {0}", myAL.Count ]; Console.WriteLine[ " Capacity : {0}", myAL.Capacity ]; Console.Write[ " Values:" ]; PrintValues[ myAL ]; // Creates a new ArrayList with seven elements and initialize each element with the string "abc". myAL = ArrayList.Repeat[ "abc", 7 ]; // Displays the count, capacity and values of the ArrayList. Console.WriteLine[ "ArrayList with seven elements with a string value" ]; Console.WriteLine[ " Count : {0}", myAL.Count ]; Console.WriteLine[ " Capacity : {0}", myAL.Capacity ]; Console.Write[ " Values:" ]; PrintValues[ myAL ]; } public static void PrintValues[ IEnumerable myList ] { foreach [ Object obj in myList ] Console.Write[ " {0}", obj ]; Console.WriteLine[]; } } /* This code produces the following output. ArrayList with five elements with a null value Count : 5 Capacity : 16 Values: ArrayList with seven elements with a string value Count : 7 Capacity : 16 Values: abc abc abc abc abc abc abc */ Imports System.Collections Public Class SamplesArrayList Public Shared Sub Main[] ' Creates a new ArrayList with five elements and initialize each ' element with a null value. Dim myAL As ArrayList = ArrayList.Repeat[Nothing, 5] ' Displays the count, capacity and values of the ArrayList. Console.WriteLine["ArrayList with five elements with a null value"] Console.WriteLine[" Count : {0}", myAL.Count] Console.WriteLine[" Capacity : {0}", myAL.Capacity] Console.Write[" Values:"] PrintValues[myAL] ' Creates a new ArrayList with seven elements and initialize each ' element with the string "abc". myAL = ArrayList.Repeat["abc", 7] ' Displays the count, capacity and values of the ArrayList. Console.WriteLine["ArrayList with seven elements with a string value"] Console.WriteLine[" Count : {0}", myAL.Count] Console.WriteLine[" Capacity : {0}", myAL.Capacity] Console.Write[" Values:"] PrintValues[myAL] End Sub Public Shared Sub PrintValues[myList As IEnumerable] Dim obj As [Object] For Each obj In myList Console.Write[" {0}", obj] Next obj Console.WriteLine[] End Sub End Class ' This code produces the following output. ' ' ArrayList with five elements with a null value ' Count : 5 ' Capacity : 16 ' Values: ' ArrayList with seven elements with a string value ' Count : 7 ' Capacity : 16 ' Values: abc abc abc abc abc abc abcRemarks
ArrayList accepts null as a valid value and allows duplicate elements.
This method is an O[n] operation, where n is count.