Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ArrayList.Repeat Method

Returns an ArrayList whose elements are copies of the specified value.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public static ArrayList Repeat (
	Object value,
	int count
)
public static ArrayList Repeat (
	Object value, 
	int count
)
public static function Repeat (
	value : Object, 
	count : int
) : ArrayList
Not applicable.

Parameters

value

The Object to copy multiple times in the new ArrayList. The value can be a null reference (Nothing in Visual Basic).

count

The number of times value should be copied.

Return Value

An ArrayList with count number of elements, all of which are copies of value.
Exception typeCondition

ArgumentOutOfRangeException

count is less than zero.

ArrayList accepts a null reference (Nothing in Visual Basic) as a valid value and allows duplicate elements.

This method is an O(n) operation, where n is count.

The following code example shows how to create and initialize a new ArrayList with the same value.

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

*/ 

import System.*;
import System.Collections.*;

public class SamplesArrayList
{
    public static void main(String[] args)
    {
        // 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}", (Int32)myAL.get_Count());
        Console.WriteLine("   Capacity : {0}", (Int32)myAL.get_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}", (Int32)myAL.get_Count());
        Console.WriteLine("   Capacity : {0}", (Int32)myAL.get_Capacity());
        Console.Write("   Values:");
        PrintValues(myAL);
    } //main

    public static void PrintValues(IEnumerable myList)
    {
        IEnumerator objMyEnum = myList.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            Console.Write("   {0}", obj);
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArrayList 
/* 
 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

 */

import System;
import System.Collections;

// Creates a new ArrayList with five elements and initialize each element with a null value.
var myAL : ArrayList = 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 );

function PrintValues( myList : IEnumerable )  {
   var myEnumerator : System.Collections.IEnumerator  = myList.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.Write( "\t{0}", myEnumerator.Current );
   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
 */ 

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.