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허용됩니다.

하는 경우 index 값과 같음 Countvalue 끝에 추가 됩니다 StringCollection합니다.

목록처럼 연속된 요소로 구성된 컬렉션에서는 삽입 지점 다음에 있는 요소를 아래로 이동하여 새 요소로 적용합니다. 컬렉션에 인덱스가 지정된 경우 이동되는 요소의 인덱스도 업데이트됩니다. 요소가 해시 테이블 같은 버킷으로 개념적으로 그룹화되어 있는 컬렉션에는 이러한 동작이 적용되지 않습니다.

이 메서드는 O (n) 작업, 여기서 nCount합니다.

적용 대상

추가 정보