次の方法で共有


ArrayList.Repeat メソッド

要素が指定した値のコピーである ArrayList を返します。

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

パラメータ

  • value
    新しい ArrayList で複数回コピーする Object 。値は null 参照 (Visual Basic では Nothing) に設定できます。
  • count
    value をコピーする回数。

戻り値

count 個の要素がある ArrayList 。要素はすべて value のコピーです。

例外

例外の種類 条件
ArgumentOutOfRangeException count が 0 未満です。

解説

ArrayList は、 null 参照 (Visual Basic では Nothing) を有効な値として受け取り、要素の重複を許可します。

使用例

同一の値を使用して新しい 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 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
 */ 

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

ArrayList クラス | ArrayList メンバ | System.Collections 名前空間