C#でSortedListオブジェクトの値を取得する
SortedListオブジェクトの値を取得するためのコードは、次のとおりです-
例
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList list = new SortedList();
list.Add(1, "One");
list.Add(2, "Two");
list.Add(3, "Three");
list.Add(4, "Four");
list.Add(5, "Five");
ICollection col1 = list.Values;
Console.WriteLine("Values...");
foreach(string s in col1)
Console.WriteLine(s);
ICollection col2 = list.Keys;
Console.WriteLine("Keys...");
foreach(int s in col2)
Console.WriteLine(s);
}
} 出力
これにより、次の出力が生成されます-
Values... One Two Three Four Five Keys... 1 2 3 4 5
例
別の例を見てみましょう-
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList list = new SortedList();
list.Add("A", "Jacob");
list.Add("B", "Sam");
list.Add("C", "Tom");
list.Add("D", "John");
list.Add("E", "Tim");
list.Add("F", "Mark");
list.Add("G", "Gary");
list.Add("H", "Nathan");
list.Add("I", "Shaun");
list.Add("J", "David");
ICollection col1 = list.Values;
Console.WriteLine("Values...");
foreach(string s in col1)
Console.WriteLine(s);
ICollection col2 = list.Keys;
Console.WriteLine("\nKeys...");
foreach(string s in col2)
Console.WriteLine(s);
}
} 出力
これにより、次の出力が生成されます-
Values... Jacob Sam Tom John Tim Mark Gary Nathan Shaun David Keys... A B C D E F G H I J
-
C#のSortedListクラスとは何ですか?
ソートされたリストは、配列とハッシュテーブルの組み合わせです。キーまたはインデックスを使用してアクセスできるアイテムのリストが含まれています。インデックスを使用してアイテムにアクセスする場合、それはArrayListであり、キーを使用してアイテムにアクセスする場合、それはハッシュテーブルです。アイテムのコレクションは常にキー値で並べ替えられます。 SortedList-に4つのキーと値のペアを追加した例を見てみましょう。 例 using System; using System.Collections; namespace Demo { class Program
-
C#のSortedListクラスのValuesプロパティとは何ですか?
まず、SortedListクラスを宣言します- SortedList list = new SortedList(); 次に、値を追加します- list.Add("S1", "Wallets"); list.Add("S2", "Sunglasses"); list.Add("S3", "Backpacks"); 以下は、SortedListクラスのValuesプロパティを操作するコードです- 例 using System; using System.Collection