ArrayList.Repeat 方法

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

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Function Repeat ( _
    value As Object, _
    count As Integer _
) As ArrayList
用法
Dim value As Object
Dim count As Integer
Dim returnValue As ArrayList

returnValue = ArrayList.Repeat(value, count)
public static ArrayList Repeat (
    Object value,
    int count
)
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

参数

  • value
    要在新 ArrayList 中对其进行多次复制的 Object。该值可以为 空引用(在 Visual Basic 中为 Nothing)。
  • count
    value 应被复制的次数。

返回值

具有 count 所指定的元素数的 ArrayList,其中的所有元素都是 value 的副本。

异常

异常类型 条件

ArgumentOutOfRangeException

count 小于零。

备注

ArrayList 接受 空引用(在 Visual Basic 中为 Nothing) 作为有效值并且允许重复的元素。

此方法的运算复杂度为 O(n),其中 n 是 count。

示例

下面的代码示例演示如何用同一个值创建和初始化新 ArrayList

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 obj As [Object]
        For Each obj In  myList
            Console.Write("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

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
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

*/ 
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

 */
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 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ArrayList 类
ArrayList 成员
System.Collections 命名空间