ArrayList.Repeat(Object, Int32) 方法

定义

返回 ArrayList,其元素是指定值的副本。

public:
 static System::Collections::ArrayList ^ Repeat(System::Object ^ value, int count);
public static System.Collections.ArrayList Repeat (object value, int count);
public static System.Collections.ArrayList Repeat (object? value, int count);
static member Repeat : obj * int -> System.Collections.ArrayList
Public Shared Function Repeat (value As Object, count As Integer) As ArrayList

参数

value
Object

要在新的 ArrayList 中对其进行多次复制的 Object。 该值可以为 null

count
Int32

value 应复制的次数。

返回

具有 count 个元素数的 ArrayList,所有元素都是 value 的副本。

例外

count 小于零。

示例

下面的代码示例演示如何创建和初始化具有相同值的新 ArrayList

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<Object^>(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	abc

注解

ArrayList 接受 null 为有效值,并允许重复的元素。

此方法是一个 O(n) 操作,其中 ncount

适用于