Export (0) Print
Expand All

ArrayList.Repeat Method

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

[Visual Basic]
Public Shared Function Repeat( _
   ByVal value As Object, _
   ByVal count As Integer _
) As ArrayList
[C#]
public static ArrayList Repeat(
 object value,
 int count
);
[C++]
public: static ArrayList* Repeat(
 Object* value,
 int count
);
[JScript]
public static function Repeat(
   value : Object,
 count : int
) : ArrayList;

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.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException count is less than zero.

Remarks

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

Example

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

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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 'Main
    
    
    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        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    abc

[C#] 
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 )  {
      System.Collections.IEnumerator myEnumerator = 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
*/ 

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

#define NULL 0

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( 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}", __box(myAL->Count) );
       Console::WriteLine( "   Capacity : {0}", __box(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( S"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}", __box(myAL->Count) );
       Console::WriteLine( "   Capacity : {0}", __box(myAL->Capacity) );
       Console::Write( "   Values:" );
       PrintValues( myAL );
    }
 
void PrintValues( IEnumerable* myList )  {
       System::Collections::IEnumerator* myEnumerator = 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
 */ 

[JScript] 
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
 */ 

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

ArrayList Class | ArrayList Members | System.Collections Namespace

Show:
© 2014 Microsoft