為什么要引入集合概念:
演示數據的局限性
1.數組的局限性
數組只能存儲相同類型的數據,比如int[]數組只能存儲int數據
2.數組存在大量垃圾數據
3.數組不能動態的擴展長度
集合
Hashtable
ArrayList
Stack
Queue
泛型集合
ArrayList?-->?List
ArrayList?al?=?new?ArrayList();
List?li?=?new?List?();
HashTable?-->?Dictionary
HashTable
key1--->value
key2--->value
這里面key是唯一的
遍歷所有key--value
Hashtable?ht?=?new?Hashtable();
Person?p1?=?new?Person?();
p1.user_Name?=?"馬志龍";
p1.user_Id?=?9527;
Person?p2?=?new?Person?();
p2.user_Name?=?"尹露";
p2.user_Id?=?9528;
ht.Add?(p1.user_Id,?p1);
ht.Add?(p2.user_Id,?p2);
獲取字段枚舉遍歷器
IDictionaryEnumerator?ide?=?ht.GetEnumerator?();
while?(ide.MoveNext?())?{
ide.Entry
將取出的內容轉換成字典實體
DictionaryEntry?de?=?(DictionaryEntry)ide.Current;
通過字典實體內的字段value,取出當時你存value時候的對象P,取出來的對象
需要轉換(和你當時存的value類型是一致的)
Person?per?=?de.Value?as?Person;
Console.WriteLine?("用戶的id:{0},用戶的姓名:{1}",?per.user_Id,?per.user_Name);
}
*******************************************stack**********************************************
stack先進后出的管理方式,不安全的集合(他可以存儲多種不同的元素)
裝箱和拆箱
裝就是把普通數據類型轉成object對象類型
拆就是把裝好的object拆成創想之前的真實類型(強制轉換)
占用內存,消耗內存太大
?[?g?6??v'tt