StringCollection.Insert(Int32, String) メソッド

定義

StringCollection 内の指定したインデックス位置に、文字列を挿入します。

public:
 void Insert(int index, System::String ^ value);
public void Insert (int index, string value);
public void Insert (int index, string? value);
member this.Insert : int * string -> unit
Public Sub Insert (index As Integer, value As String)

パラメーター

index
Int32

value を挿入する位置の 0 から始まるインデックス。

value
String

挿入する文字列。 値として null を指定できます。

例外

index が 0 未満です。

- または -

indexCount より大きい。

次のコード例では、 に新しい要素を追加します StringCollection

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintValues( IEnumerable^ myCol );
int main()
{
   
   // Creates and initializes a new StringCollection.
   StringCollection^ myCol = gcnew StringCollection;
   Console::WriteLine( "Initial contents of the StringCollection:" );
   PrintValues( myCol );
   
   // Adds a range of elements from an array to the end of the StringCollection.
   array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};
   myCol->AddRange( myArr );
   Console::WriteLine( "After adding a range of elements:" );
   PrintValues( myCol );
   
   // Adds one element to the end of the StringCollection and inserts another at index 3.
   myCol->Add( "* white" );
   myCol->Insert( 3, "* gray" );
   Console::WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" );
   PrintValues( myCol );
}

void PrintValues( IEnumerable^ myCol )
{
   IEnumerator^ myEnum = myCol->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   {0}", obj );
   }

   Console::WriteLine();
}

/*
This code produces the following output.

Initial contents of the StringCollection:

After adding a range of elements:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After adding "* white" to the end and inserting "* gray" at index 3:
   RED
   orange
   yellow
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

*/
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringCollection  {

   public static void Main()  {

      // Creates and initializes a new StringCollection.
      StringCollection myCol = new StringCollection();

      Console.WriteLine( "Initial contents of the StringCollection:" );
      PrintValues( myCol );

      // Adds a range of elements from an array to the end of the StringCollection.
      String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
      myCol.AddRange( myArr );

      Console.WriteLine( "After adding a range of elements:" );
      PrintValues( myCol );

      // Adds one element to the end of the StringCollection and inserts another at index 3.
      myCol.Add( "* white" );
      myCol.Insert( 3, "* gray" );

      Console.WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" );
      PrintValues( myCol );
   }

   public static void PrintValues( IEnumerable myCol )  {
      foreach ( Object obj in myCol )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }
}

/*
This code produces the following output.

Initial contents of the StringCollection:

After adding a range of elements:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After adding "* white" to the end and inserting "* gray" at index 3:
   RED
   orange
   yellow
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

*/
Imports System.Collections
Imports System.Collections.Specialized

Public Class SamplesStringCollection   

   Public Shared Sub Main()

      ' Creates and initializes a new StringCollection.
      Dim myCol As New StringCollection()

      Console.WriteLine("Initial contents of the StringCollection:")
      PrintValues(myCol)

      ' Adds a range of elements from an array to the end of the StringCollection.
      Dim myArr() As [String] = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
      myCol.AddRange(myArr)

      Console.WriteLine("After adding a range of elements:")
      PrintValues(myCol)

      ' Adds one element to the end of the StringCollection and inserts another at index 3.
      myCol.Add("* white")
      myCol.Insert(3, "* gray")

      Console.WriteLine("After adding ""* white"" to the end and inserting ""* gray"" at index 3:")
      PrintValues(myCol)

   End Sub

   Public Shared Sub PrintValues(myCol As IEnumerable)
      Dim obj As [Object]
      For Each obj In  myCol
         Console.WriteLine("   {0}", obj)
      Next obj
      Console.WriteLine()
   End Sub

End Class


'This code produces the following output.
'
'Initial contents of the StringCollection:
'
'After adding a range of elements:
'   RED
'   orange
'   yellow
'   RED
'   green
'   blue
'   RED
'   indigo
'   violet
'   RED
'
'After adding "* white" to the end and inserting "* gray" at index 3:
'   RED
'   orange
'   yellow
'   * gray
'   RED
'   green
'   blue
'   RED
'   indigo
'   violet
'   RED
'   * white
'

注釈

で重複する文字列を StringCollection使用できます。

が とCountvalue等しい場合indexは、 のStringCollection末尾に が追加されます。

リストなどの連続する要素のコレクションでは、新しい要素を挿入するために、挿入位置より後にある要素の位置が繰り下げられます。 コレクションにインデックスがある場合、移動した要素のインデックスも更新されます。 この動作は、要素が概念的にバケットにグループ化されているハッシュ テーブルなどのコレクションには適用されません。

このメソッドは O(n) 操作です。nCount です。

適用対象

こちらもご覧ください