day 14 集合(2)
1,ArrayList是線性數據結構---->對應的泛型線性結構爲List<T>
模擬foreach循環原理
IEnumerator ie = al.GetEnumerator();
while (ie.MoveNext())
{
Student s = (Student) ie.Current;
Console.WriteLine(s.stu_name + ":" + s.stu_id + ":" + s.stu_score);
}
泛型集合List<T>
爲什麼使用泛型
1,泛型集合比非泛型集合對於數據這一塊來說,是安全的,沒有髒數據
2,在一定數據量的前提下,泛型比非泛型寫入數據和讀取數據要快的多
3,泛型集合處理數據快的原因是少了裝箱和拆箱的操作
初始化List集合,限定內部數據僅爲int
List<int> list = new List<int>();
2,哈希表(字典)key-value
Hashtable
Dictionary<TKEY,TVALUE>
key唯一
key不能對應多個value,但是value可以對應多個key
Hashtable ht = new Hashtable();
遍歷所有的鍵 foreach (var item in ht.Keys)
遍歷所有的值 foreach (var item in ht.Values)
遍歷全部key-value DictionaryEntry 字典實體-->內部的內容
foreach (DictionaryEntry item in ht) {
int key = (int)item.Key;
Student values = (Student)item.Value;
Console.WriteLine("Key值爲{0},{1}",key,values.Name);}
遍歷4 IDictionaryEnumerator ide= ht.GetEnumerator();
while (ide.MoveNext()){
DictionaryEntry de = (DictionaryEntry)ide.Current;
int key = (int)de.Key;
Student s1=de.Value as Student;
Console.WriteLine(s1.Name+s1.Score);}
3,Stack--堆棧 Stack<T>
Stack sk = new Stack();
Push() |
將對象插入Stack的頂部.(入棧操作) |
Pop() |
移除並返回Stack頂部的對象.(出棧操作) |
Peek() |
返回位於stack頂部的對象,但不移除. |
Contains() |
確定某元素是否在棧中 |
Clear() |
從statck中移除所有對象 |
Count |
獲取棧中包含的元素 |
4,Queue--隊列
Queue que = new Queue();
Enqueue() |
將對象添加到Queue的結尾處,入隊. |
Dequeue() |
移除並返回位於Queue開始處的對象 |
Peek() |
返回位於Queue開始處的對象但不將其移除 |
Contains() |
確定某元素是否在Queue中 |
Clear() |
從Queue中移除所有對象 |
Count |
獲取Queue中包含的元素數 |
5,索引器
作用:讓對象具有快速訪問元素的能力
索引器和數組的區別:
1,索引器的索引類型(index)不限定整數
2,索引器支持方法重載
3,索引器實際上跟屬性是一樣的,也屬於特殊的方法
同樣具備set和get訪問器
索引器和屬性的區別
1,索引器是以關鍵字this來標識的,屬性數任意字符,
首字母大寫
2,索引器可以重載,屬性不能
3,索引器是可以有參數的,但是屬性沒有
4,索引器不能用static修飾,但是屬性可以