C#
 Computer >> コンピューター >  >> プログラミング >> C#

SortedList要素をC#の配列オブジェクトにコピーする


SortedList要素を配列オブジェクトにコピーするためのコードは次のとおりです-

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args){
      SortedList list = new SortedList();
      list.Add("1", "AB");
      list.Add("2", "CD");
      list.Add("3", "EF");
      list.Add("4", "GH");
      list.Add("5", "IJ");
      list.Add("6", "JK");
      list.Add("7", "KL");
      list.Add("8", "LM");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list){
         Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("\nCopied to Array object...");
      DictionaryEntry[] dictArr = new DictionaryEntry[10];
      list.CopyTo(dictArr, 2);
      for (int i = 0; i < dictArr.Length; i++) {
         Console.WriteLine("Key = "+ dictArr[i].Key + ", Value = " + dictArr[i].Value);
      }
   }
}

出力

これにより、次の出力が生成されます-

SortedList elements...
Key = 1, Value = AB
Key = 2, Value = CD
Key = 3, Value = EF
Key = 4, Value = GH
Key = 5, Value = IJ
Key = 6, Value = JK
Key = 7, Value = KL
Key = 8, Value = LM

Copied to Array object...
Key = , Value =
Key = , Value =
Key = 1, Value = AB
Key = 2, Value = CD
Key = 3, Value = EF
Key = 4, Value = GH
Key = 5, Value = IJ
Key = 6, Value = JK
Key = 7, Value = KL
Key = 8, Value = LM

別の例を見てみましょう-

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args){
      SortedList list = new SortedList();
      list.Add("1", "Katie");
      list.Add("2", "Andy");
      list.Add("3", "Mark");
      list.Add("4", "Gary");
      list.Add("5", "Sam");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list){
         Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("\nCopied to Array object...");
      DictionaryEntry[] dictArr = new DictionaryEntry[5];
      list.CopyTo(dictArr, 0);
      for (int i = 0; i < dictArr.Length; i++) {
         Console.WriteLine("Key = "+ dictArr[i].Key + ", Value = " + dictArr[i].Value);
      }
   }
}

出力

これにより、次の出力が生成されます-

SortedList elements...
Key = 1, Value = Katie
Key = 2, Value = Andy
Key = 3, Value = Mark
Key = 4, Value = Gary
Key = 5, Value = Sam

Copied to Array object...
Key = 1, Value = Katie
Key = 2, Value = Andy
Key = 3, Value = Mark
Key = 4, Value = Gary
Key = 5, Value = Sam

  1. C#のSortedListクラスのItemプロパティとは何ですか?

    ソートされたリストは、配列とハッシュテーブルの組み合わせです。キーまたはインデックスを使用してアクセスできるアイテムのリストが含まれています。 SortedList内の特定のキーに関連付けられた値を取得および設定します。 Itemプロパティを使用して、新しい要素を追加することもできます。 キーが存在しない場合は、次のように含めることができます。 myCollection["myNonexistentKey"] = myValue キーがすでに存在する場合は、新しいキーと値で上書きされます。 以下は、C#でSorteListクラスのItemプロパティを操作する方法を

  2. C#のジャグ配列の要素のタイプは何ですか?

    ジャグ配列は配列の配列であるため、その要素は参照型であり、nullに初期化されます。 ジャグ配列の操作方法を見てみましょう- ジャグ配列を宣言する- int [][] marks; ここで、それを初期化します。ここで、marksは5つの整数の配列です- int[][] marks = new int[][]{new int[]{ 40,57 },new int[]{ 34,55 }, new int[]{ 23,44 },new int[]{ 56, 78 }, new int[]{ 66, 79 } }; ここで、C#でのジャグ配列の完全な例を見て、それを実装する方法を学びましょう-