SortedList.CopyTo メソッドとは? わかりやすく解説

SortedList.CopyTo メソッド

1 次元Array インスタンス指定したインデックスSortedList要素コピーします

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable Sub CopyTo
 ( _
    array As Array, _
    arrayIndex As Integer _
)
Dim instance As SortedList
Dim array As Array
Dim arrayIndex As Integer

instance.CopyTo(array, arrayIndex)
public void CopyTo (
    Array array, 
    int arrayIndex
)

パラメータ

array

SortedList から DictionaryEntry オブジェクトコピーされる 1 次元ArrayArray には、0 から始まるインデックス番号が必要です。

arrayIndex

コピー開始位置となる、array の 0 から始まるインデックス番号

例外例外
例外種類条件

ArgumentNullException

arraynull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

arrayIndex が 0 未満です。

ArgumentException

array多次元です。

または

arrayIndexarray長さ上です。

または

コピー元の SortedList要素数が、arrayIndex からコピー先の array末尾までに格納できる数を超えてます。

InvalidCastException

コピー元の SortedList の型をコピー先の array の型に自動的にキャストできません。

解説解説

キー/値ペアは、列挙子が SortedList反復処理するのと同じ順序で、Arrayコピーされます。

SortedList 内のキーだけをコピーするには、SortedList.Keys.CopyTo使用します

SortedList 内の値だけをコピーするには、SortedList.Values.CopyTo使用します

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

使用例使用例

SortedList 内の値を 1 次元Arrayコピーする方法の例を次に示します

Imports System
Imports System.Collections

Public Class SamplesSortedList

   Public Shared Sub Main()

      ' Creates and initializes the source SortedList.
      Dim mySourceList As New
 SortedList()
      mySourceList.Add(2, "cats")
      mySourceList.Add(3, "in")
      mySourceList.Add(1, "napping")
      mySourceList.Add(4, "the")
      mySourceList.Add(0, "three")
      mySourceList.Add(5, "barn")

      ' Creates and initializes the one-dimensional target Array.
      Dim tempArray() As String
 = {"The", "quick",
 "brown", "fox", "jumped", "over", "the",
 "lazy", "dog"}
      Dim myTargetArray(14) As DictionaryEntry
      Dim i As Integer =
 0
      Dim s As String
      For Each s In  tempArray
         myTargetArray(i).Key = i
         myTargetArray(i).Value = s
         i += 1
      Next s

      ' Displays the values of the target Array.
      Console.WriteLine("The target Array contains the following
 (before and after copying):")
      PrintValues(myTargetArray, " "c)

      ' Copies the entire source SortedList to the target SortedList,
 starting at index 6.
      mySourceList.CopyTo(myTargetArray, 6)

      ' Displays the values of the target Array.
      PrintValues(myTargetArray, " "c)

   End Sub 'Main


   Public Shared Sub PrintValues(myArr()
 As DictionaryEntry, mySeparator As Char)
      Dim i As Integer
      For i = 0 To myArr.Length - 1
         Console.Write("{0}{1}", mySeparator, myArr(i).Value)
      Next i
      Console.WriteLine()
  End Sub 'PrintValues

End Class 'SamplesSortedList 


'This code produces the following output.
' 
'The target Array contains the following (before and after copying):
' The quick brown fox jumped over the lazy dog      
' The quick brown fox jumped over three napping cats in the barn

 using System;
 using System.Collections;
 public class SamplesSortedList  {
 
    public static void Main()
  {
 
       // Creates and initializes the source SortedList.
       SortedList mySourceList = new SortedList();
       mySourceList.Add( 2, "cats" );
       mySourceList.Add( 3, "in" );
       mySourceList.Add( 1, "napping" );
       mySourceList.Add( 4, "the" );
       mySourceList.Add( 0, "three" );
       mySourceList.Add( 5, "barn" );
 
       // Creates and initializes the one-dimensional target Array.
       String[] tempArray = new String[] { "The", "quick",
 "brown", "fox", "jumped", "over", "the",
 "lazy", "dog" };
       DictionaryEntry[] myTargetArray = new DictionaryEntry[15];
       int i = 0;
       foreach ( String s in tempArray )  {
          myTargetArray[i].Key = i;
          myTargetArray[i].Value = s;
          i++;
       }

       // Displays the values of the target Array.
       Console.WriteLine( "The target Array contains the following (before and
 after copying):" );
       PrintValues( myTargetArray, ' ' );
 
       // Copies the entire source SortedList to the target SortedList,
 starting at index 6.
       mySourceList.CopyTo( myTargetArray, 6 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
    }
 
    public static void PrintValues(
 DictionaryEntry[] myArr, char mySeparator )  {
       for ( int i = 0; i < myArr.Length;
 i++ )
          Console.Write( "{0}{1}", mySeparator, myArr[i].Value );
       Console.WriteLine();
    }

 }


/*
This code produces the following output.
 
The target Array contains the following (before and after copying):
 The quick brown fox jumped over the lazy dog      
 The quick brown fox jumped over three napping cats in the barn

*/ 
using namespace System;
using namespace System::Collections;
void PrintValues( array<DictionaryEntry>^ myArr, Char mySeparator
 );
int main()
{
   
   // Creates and initializes the source SortedList.
   SortedList^ mySourceList = gcnew SortedList;
   mySourceList->Add( 2, "cats" );
   mySourceList->Add( 3, "in" );
   mySourceList->Add( 1, "napping" );
   mySourceList->Add( 4, "the" );
   mySourceList->Add( 0, "three" );
   mySourceList->Add( 5, "barn" );
   
   // Creates and initializes the one-dimensional target Array.
   array<String^>^tempArray = {"The","quick","brown"
,"fox","jumped","over","the","lazy"
,"dog"};
   array<DictionaryEntry>^myTargetArray = gcnew array<DictionaryEntry>(15);
   int i = 0;
   IEnumerator^ myEnum = tempArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      String^ s = safe_cast<String^>(myEnum->Current);
      myTargetArray[ i ].Key = i;
      myTargetArray[ i ].Value = s;
      i++;
   }

   
   // Displays the values of the target Array.
   Console::WriteLine( "The target Array contains the following (before and
 after copying):" );
   PrintValues( myTargetArray, ' ' );
   
   // Copies the entire source SortedList to the target SortedList,
 starting at index 6.
   mySourceList->CopyTo( myTargetArray, 6 );
   
   // Displays the values of the target Array.
   PrintValues( myTargetArray, ' ' );
}

void PrintValues( array<DictionaryEntry>^ myArr, Char mySeparator
 )
{
   for ( int i = 0; i < myArr->Length;
 i++ )
      Console::Write( "{0}{1}", mySeparator, myArr[ i ].Value );
   Console::WriteLine();
}

/*
This code produces the following output.
 
The target Array contains the following (before and after copying):
 The quick brown fox jumped over the lazy dog      
 The quick brown fox jumped over three napping cats in the barn

*/
import System.*;
import System.Collections.*;

public class SamplesSortedList
{

    public static void main(String[]
 args)
    {
        // Creates and initializes the source SortedList.
        SortedList mySourceList = new SortedList();

        mySourceList.Add((Int32)2, "cats");
        mySourceList.Add((Int32)3, "in");
        mySourceList.Add((Int32)1, "napping");
        mySourceList.Add((Int32)4, "the");
        mySourceList.Add((Int32)0, "three");
        mySourceList.Add((Int32)5, "barn");

        // Creates and initializes the one-dimensional target Array.
        String tempArray[] = new String[] {"The", "quick",
 "brown", "fox", 
            "jumped", "over", "the", "lazy",
 "dog"};
        DictionaryEntry myTargetArray[] = new DictionaryEntry[15];

        for (int iCtr = 0; iCtr < tempArray.length;
 iCtr++) {
            String s = tempArray[iCtr];
            myTargetArray[iCtr].set_Key((Int32)iCtr);
            myTargetArray[iCtr].set_Value(s);
        }

        // Displays the values of the target Array.
        Console.WriteLine("The target Array contains the following " 
            + "(before and after copying):");
        PrintValues(myTargetArray, ' ');

        // Copies the entire source SortedList to the target SortedList,
 
        // starting at index 6.
        mySourceList.CopyTo(myTargetArray, 6);

        // Displays the values of the target Array.
        PrintValues(myTargetArray, ' ');
    } //main

    public static void PrintValues(DictionaryEntry[]
 myArr, char mySeparator)
    {
        for (int i = 0; i < myArr.length;
 i++) {
            Console.Write("{0}{1}", 
                System.Convert.ToString(mySeparator), 
                System.Convert.ToString(myArr[i].get_Value()));
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesSortedList

/*
This code produces the following output.
 
The target Array contains the following (before and after copying):
 The quick brown fox jumped over the lazy dog      
 The quick brown fox jumped over three napping cats in the barn

 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からSortedList.CopyTo メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からSortedList.CopyTo メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からSortedList.CopyTo メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

SortedList.CopyTo メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SortedList.CopyTo メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS