将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元素...-");
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元素...- 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元素...-");
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元素...- 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
热门推荐
2 祝福语简短七夕
10 宝宝新年祝福语大全简短
11 结婚婚庆祝福语简短
12 老师节祝福语的简短
13 红包祝福语简短网名
14 古风婚礼祝福语唯美简短
15 周末愉快祝福语高级简短
16 送花祝福语简短十一朵
17 简短的高考祝福语精选
18 猪年幽默祝福语简短