C#数据集合Word文档格式.docx
《C#数据集合Word文档格式.docx》由会员分享,可在线阅读,更多相关《C#数据集合Word文档格式.docx(9页珍藏版)》请在冰豆网上搜索。
usingSystem.Collections;
namespaceConsoleApplication1
...{
classProgram
...{
staticvoidMain(string[]args)
ArrayListal=newArrayList();
al.Add(100);
//单个添加
foreach(intnumberinnewint[6]...{9,3,7,2,4,8})
al.Add(number);
//集体添加方法一
}
int[]number2=newint[2]...{11,12};
al.AddRange(number2);
//集体添加方法二
al.Remove(3);
//移除值为3的
al.RemoveAt(3);
//移除第3个
ArrayListal2=newArrayList(al.GetRange(1,3));
//新ArrayList只取旧ArrayList一部份
Console.WriteLine("
遍历方法一:
"
);
foreach(intiinal)//不要强制转换
Console.WriteLine(i);
//遍历方法一
遍历方法二:
for(inti=0;
i!
=al2.Count;
i++)//数组是length
intnumber=(int)al2[i];
//一定要强制转换
Console.WriteLine(number);
//遍历方法二
}
图1
2.Stack类
栈,后进先出。
push方法入栈,pop方法出栈。
Stacksk=newStack();
Stacksk2=newStack();
foreach(intiinnewint[4]...{1,2,3,4})
sk.Push(i);
//填充
sk2.Push(i);
foreach(intiinsk)
//遍历
sk.Pop();
Pop"
sk2.Peek();
//弹出最后一项不删除
Peek"
foreach(intiinsk2)
while(sk2.Count!
=0)
inti=(int)sk2.Pop();
//清空
sk2.Pop();
清空"
图2
#p#
3.Queue类
队列,先进先出。
enqueue方法入队列,dequeue方法出队列。
Queuequ=newQueue();
Queuequ2=newQueue();
qu.Enqueue(i);
qu2.Enqueue(i);
foreach(intiinqu)
qu.Dequeue();
Dequeue"
qu2.Peek();
//弹出最后一项不删除
foreach(intiinqu2)
while(qu2.Count!
inti=(int)qu2.Dequeue();
qu2.Dequeue();
图3
4.Hashtable类
哈希表,名-值对。
类似于字典(比数组更强大)。
哈希表是经过优化的,访问下标的对象先散列过。
如果以任意类型键值访问其中元素会快于其他集合。
GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。
哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。
publicstaticvoidMain()
//CreatesandinitializesanewHashtable.
HashtablemyHT=newHashtable();
myHT.Add("
one"
"
The"
two"
quick"
three"
brown"
four"
fox"
//DisplaystheHashtable.
TheHashtablecontainsthefollowing:
PrintKeysAndValues(myHT);
publicstaticvoidPrintKeysAndValues(HashtablemyHT)
foreach(stringsinmyHT.Keys)
Console.WriteLine(s);
-KEY--VALUE-"
foreach(DictionaryEntrydeinmyHT)
{0}:
{1}"
de.Key,de.Value);
Console.WriteLine();
图4
5.SortedList类
与哈希表类似,区别在于SortedList中的Key数组排好序的。
SortedListsl=newSortedList();
sl["
c"
]=41;
a"
]=42;
d"
]=11;
b"
]=13;
foreach(DictionaryEntryelementinsl)
strings=(string)element.Key;
inti=(int)element.Value;
{0},{1}"
s,i);
图5
6.NameValueCollection类
官方给NameValueCollection定义为特殊集合一类,在System.Collections.Specialized下。
System.Collections.Specialized下还有HybridDicionary类,建议少于10个元素用HybridDicionary,当元素增加会自动转为HashTable。
System.Collections.Specialized下还有HybridDicionary类,字符串集合。
System.Collections.Specialized下还有其他类大家可以各取所需!
言归正转主要说NameValueCollection,HashTable和NameValueCollection很类似但是他们还是有区别的,HashTable的KEY是唯一性,而NameValueCollection则不唯一!
usingSystem.Collections.Specialized;
System.Collections.Hashtableht=newSystem.Collections.Hashtable();
ht.Add("
DdpMDisplaySeq"
.Trim(),"
DisplaySequence"
.Trim());
DdpMNameChi"
Name(Chinese)"
DdpMNameEng"
Name(English)"
Comment"
DdpMMarketCode"
MarketCode"
foreach(objectkeyinht.Keys)
{0}/{1}{2},{3}"
key,ht[key],
key.GetHashCode(),ht[key].GetHashCode());
"
NameValueCollectionmyCol=newNameValueCollection();
myCol.Add("
foreach(stringkeyinmyCol.Keys)
{0}/{1}{2},{3}"
key,myCol[key],
key.GetHashCode(),myCol[key].GetHashCode());