




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、第3章 集合類C#程序設(shè)計(jì)基礎(chǔ)回顧n屬性分為三類:只讀屬性、只寫屬性和讀寫屬性。n同一個(gè)類中多個(gè)方法使用同一名字,該方法就被稱為重載方法。 nC# 構(gòu)造方法是一種特殊的方法,構(gòu)造方法的名字必須與定義它的類名完全相同,并且沒有返回類型。n構(gòu)造方法是在使用 new 關(guān)鍵字創(chuàng)建對(duì)象時(shí)系統(tǒng)自動(dòng)調(diào)用的n構(gòu)造方法是在初始化對(duì)象時(shí)給對(duì)象成員賦初值12341234集合概述列表集合ArrayList字典集合Hashtable集合類的應(yīng)用了解集合使用列表集合ArrayList裝載三國人物使用列表集合Hashtable裝載三國人物集合類的綜合運(yùn)用講解時(shí)間: 分鐘實(shí)踐時(shí)間: 分鐘技術(shù)講解實(shí)踐練習(xí)集合概述1.集合的主要
2、特征與類型2.數(shù)組的缺陷1.了解集合25203.1.1 集合的主要特征與類型n在.NET 平臺(tái)封裝好了一些常用的數(shù)據(jù)結(jié)構(gòu),稱為集合類,簡稱“集合”n C# 中的集合表示可以通過遍歷方式來訪問一組對(duì)象,其主要特性如下:n動(dòng)態(tài)地改變大小n可存儲(chǔ)任意類型的對(duì)象n提供一系列操作集合中對(duì)象的方法3.1.1 集合的主要特征與類型nC# 中的集合被定義在 System.Collections 命名空間中,如圖 3.1 所示,如 ArrayList(數(shù)組列表)、Hashtable(字典)、Queue(隊(duì)列)、Stack(棧)和 SortedList(排序列表)等3.1.2 數(shù)組的缺陷n客戶在銀行辦理業(yè)務(wù)時(shí),常
3、常需要排隊(duì),現(xiàn)在我們定義一個(gè)數(shù)組來保存排隊(duì)等候辦理業(yè)務(wù)的客戶public class Customer private string name; / 姓名 private int age; / 年齡 private string address; / 住址 /省略構(gòu)造方法及屬性的封裝定義客戶類static void Main(string args) Customer list = new Customer2; Customer teng = new Customer( 馬騰 , 32, 汕頭市 ); Customer yun = new Customer( 王云 , 28, 杭州市 ); l
4、ist0 = teng; list1 = yun; Console.WriteLine( 排隊(duì)等待的客戶有:); for (int i = 0; i list.Length; i+) Console.WriteLine(0 號(hào):1, i+1, listi.Name); 保存排隊(duì)客戶3.1.2 數(shù)組的缺陷我們?cè)谑纠?3.1 的留白處添加下述代碼:Customerjun=newCustomer( 李軍 ,25, 仙桃市 );list2=jun;添加了此段代碼后,運(yùn)行結(jié)果將是什么?需求說明實(shí)踐時(shí)間: 分鐘3.1.3 學(xué)生實(shí)踐練習(xí)n 在實(shí)際工作中,數(shù)據(jù)的數(shù)量是可變的,這就要求有一個(gè)可以變化的容器來裝載
5、,但是數(shù)組的長度是不可變的。本案例使用控制臺(tái)應(yīng)用程序,以添加“三國時(shí)期的人物”為例,使用數(shù)組來裝載變化的數(shù)據(jù),以不變應(yīng)萬變。25實(shí)現(xiàn)思路3.1.3 學(xué)生實(shí)踐練習(xí)n 創(chuàng)建控制臺(tái)應(yīng)用程序,取名為 ThreeKingdomsn 在 Main() 方法中定義一個(gè)長度為 2 的數(shù)組,并將兩個(gè)三國人物姓名賦值給數(shù)組n 定義一個(gè)添加方法,在方法中根據(jù)原有數(shù)組的長度加 1 創(chuàng)建新的數(shù)組,并將原數(shù)組的人物姓名拷貝到新的數(shù)組,同時(shí)將要加入的數(shù)據(jù)賦值給數(shù)組的最后一位,并將新的結(jié)果返回給原數(shù)組變量n 使用 foreach 循環(huán)數(shù)組打印出數(shù)組中的數(shù)據(jù)講解時(shí)間: 分鐘實(shí)踐時(shí)間: 分鐘技術(shù)講解實(shí)踐練習(xí)列表集合 ArrayL
6、ist1.ArrayList介紹2.ArrayList的使用1.使用列表集ArrayList裝載三國人物25203.2.1 ArrayList介紹nArrayList 類似于數(shù)組,因其大小可根據(jù)需要?jiǎng)討B(tài)地改變,故也可稱為動(dòng)態(tài)數(shù)組。ArrayList 把所有存儲(chǔ)的元素都當(dāng)作對(duì)象引用,因而可以在 ArrayList 中存儲(chǔ)任何對(duì)象,但在訪問對(duì)象時(shí),需要把它們的數(shù)據(jù)類型轉(zhuǎn)換回存儲(chǔ)前的類型。3.2.2 ArrayList的使用n使用ArrayList類實(shí)現(xiàn)3.1的功能static void Main(string args) ArrayList list = new ArrayList() ; Cus
7、tomer teng = new Customer( 馬騰 , 32, 汕頭市 ); Customer yun = new Customer( 王云 , 28, 杭州市 ); Customer si= new Customer( 李軍 , 25, 仙桃市 ); list.Add(teng); list.Add(yun); list.Add(si); Console.WriteLine( 排隊(duì)等待的客戶有:); for (int i = 0; i list.Count; i+) Console.WriteLine(0 號(hào):1, i+1, (listi as Customer).Name); 使用
8、ArrayList集合代替數(shù)組3.2.2 ArrayList的使用nArrayList 的常用屬性見下表屬性功能說明Capacity 獲取或設(shè)置 ArrayList 可包含的元素個(gè)數(shù)Count 獲取 ArrayList 實(shí)際包含的元素個(gè)數(shù)n 當(dāng)有新的客戶需要排隊(duì)時(shí),可以繼續(xù)添加n 任何對(duì)象存儲(chǔ)到 ArrayList 中都會(huì)自動(dòng)轉(zhuǎn)換成 object 類型的數(shù)據(jù),所以在訪問時(shí)必須從 object 類型轉(zhuǎn)換回存儲(chǔ)前的類型。3.2.2 ArrayList的使用n下面將以用戶的添加為例,說明 Capacity 和 Count 的用法static void Main(string args) Custom
9、er jun = new Customer( 李軍 ,32, 廣州 ); / 創(chuàng)建客戶 1 Customer yun = new Customer( 王云 , 28, 杭州 ); / 創(chuàng)建客戶 2 Customer si = new Customer( 雷斯 , 43, 美國 ); / 創(chuàng)建客戶 3 ArrayList list = new ArrayList(2); Console.WriteLine( 集合可存儲(chǔ)元素?cái)?shù):0, list.Capacity); Console.WriteLine( 集合實(shí)際元素?cái)?shù):0, list.Count); list.Add(jun); Console.W
10、riteLine( 集合可存儲(chǔ)元素?cái)?shù):0, list.Capacity); Console.WriteLine( 集合實(shí)際元素?cái)?shù):0, list.Count); list.Add(yun); list.Add(si); Console.WriteLine( 集合可存儲(chǔ)元素?cái)?shù):0, list.Capacity); Console.WriteLine( 集合實(shí)際元素?cái)?shù):0, list.Count);創(chuàng)建時(shí)可以指定容量3.2.2 ArrayList的使用nArrayList的常用方法方法功能說明Add(objectobj) 將元素添加到 ArrayList 結(jié)尾處Insert(intindex,ob
11、jectobj) 將元素添加到 ArrayList 的指定索引處Remove(objectobj) 移除 ArrayList 指定的元素RemoveAt(intindex) 移除 ArrayList 指定索引處元素Clear()清除 ArrayList 中所有元素Sort()對(duì) ArrayList 中的元素排序Reverse()將 ArrayList 中的元素順序反轉(zhuǎn)TrimToSize()將 ArrayList 容量數(shù)設(shè)置為實(shí)際元素個(gè)數(shù)3.2.2 ArrayList的使用n使用ArrayList集合添加元素static void Main(string args) int num = 1;
12、Customer jun = new Customer( 李軍 ,32, 廣州 ); Customer yun = new Customer( 王云 , 28, 杭州 ); Customer si = new Customer( 雷斯 , 43, 美國 ); ArrayList list = new ArrayList(2); list.Add(jun); list.Add(yun); list.Insert(1, si); Console.WriteLine( 排隊(duì)的客戶有:); foreach (object obj in list) Customer cust = obj as Cust
13、omer;Console.WriteLine(0 號(hào)客戶:1, num+, cust.Name); 3.2.2 ArrayList的使用n使用ArrayList集合訪問元素static void Main(string args) Customer jun = new Customer( 李軍 ,32, 廣州 ); Customer yun = new Customer( 王云 , 28, 杭州 ); Customer si = new Customer( 雷斯 , 43, 美國 ); ArrayList list = new ArrayList(2); list.Add(jun); list
14、.Add(yun); list.Add(si); Customer cust = list1 as Customer; Console.WriteLine(2 號(hào)客戶:0, cust.Name);3.2.2 ArrayList的使用n根據(jù)索引,使用ArrayList集合刪除元素static void Main(string args) int num =1; /初始化客戶代碼省略 ArrayList list = new ArrayList(2); list.Add(jun); list.Add(yun); list.Add(si); list.RemoveAt(0); Console.Wri
15、teLine( 排隊(duì)的客戶有:); foreach (object obj in list) Customer cust = obj as Customer;Console.WriteLine(0 號(hào)客戶:1, num+, cust.Name); 排隊(duì)序號(hào)根據(jù)索引刪除元素3.2.2 ArrayList的使用n使用ArrayList集合通過 Sort() 和 Reverse() 方法進(jìn)行排序static void Main(string args) /初始化客戶代碼省略 num = 1; list.Sort(); Console.WriteLine( 使用 Sort() 方法排序后:); for
16、each (string cust in list) Console.WriteLine(0 號(hào)客戶:1, num+, cust); num = 1; list.Reverse(); Console.WriteLine( 使用 Reverse() 方法排序后:); /循環(huán)輸出客戶代碼省略使用sort方法進(jìn)行排序使用Reverse方法進(jìn)行反轉(zhuǎn)需求說明實(shí)踐時(shí)間: 分鐘3.2.3 學(xué)生實(shí)踐練習(xí)n 動(dòng)在實(shí)際工作中,經(jīng)常涉及到對(duì)容器中的數(shù)據(jù)進(jìn)行操作。本案例將使用 ArrayList 實(shí)現(xiàn)對(duì)三國時(shí)期人物的添加、插入、移除、排序、反轉(zhuǎn)和清除等操作。30實(shí)現(xiàn)思路3.2.3 學(xué)生實(shí)踐練習(xí)n 創(chuàng)建 Windows
17、 窗體應(yīng)用,取名為 ThreeKingdomsArrayListn 添加 Windows 窗體,命名為 Operation,并在該窗體中添加 對(duì)應(yīng)的按鈕,便于對(duì)容器的操作創(chuàng)建操作n 添加 Windows 窗體,命名為 ArrayListShow,并在窗體中添加 ListBox 控件(命名為 lbArrayList)和一個(gè)帶參構(gòu)造方法,在通過該窗體顯示三國人物信息時(shí),使用構(gòu)造方法將英雄信息傳遞到該窗體,并賦值給lbArrayList。n 定義一個(gè) ArrayList 方法集合,用于存儲(chǔ)三國人物,并實(shí)現(xiàn)點(diǎn)擊相應(yīng)的按鈕對(duì) ArrayList 中的三國人物做出對(duì)應(yīng)的操作講解時(shí)間: 分鐘實(shí)踐時(shí)間: 分鐘
18、技術(shù)講解實(shí)踐練習(xí)字典集合 Hashtable1.ArrayList的局限性2.Hashtable的使用1.使用列表集Hashtable裝載三國人物25203.3.1 ArrayList的局限性nHashtable 是用于處理 key/value( 鍵 / 值 ) 對(duì)的集合容器。其中,key 通常用于快速查找,value 用于存儲(chǔ)對(duì)應(yīng)于 key 的值,key 區(qū)分大小寫。Hashtable 中鍵值均為 object 類型。3.3.1 ArrayList的局限性n在ArrayList集合中找到指定元素static void Main(string args) /初始化客戶代碼省略 string n
19、ame = 王云 ; for(int i=0;ilist.Count;i+) Customer cust = listi as Customer;if (cust.Name.Equals(name) Console.WriteLine(0 客戶在排隊(duì),其住址是:1, name, cust.Address); break; 需要查找的客戶姓名查找姓名為王云的客戶是否在排隊(duì),如果找到則顯示其住址3.3.2 Hashtable的使用n使用Hashtable實(shí)現(xiàn)上述案例static void Main(string args) Customer jun = new Customer( 李軍 ,32,
20、廣州 ); Customer yun = new Customer( 王云 , 28, 杭州 ); Customer si = new Customer( 雷斯 , 43, 美國 ); Hashtable list = new Hashtable(); list.Add(jun.Name, jun); list.Add(yun.Name, yun); list.Add(si.Name, si); string name = 王云 ; Customer cust = listname as Customer; Console.WriteLine(0 客戶在排隊(duì),其住址是:1, name, cus
21、t.Address);把客戶的姓名存為 key,客戶對(duì)象存為 value3.3.2 Hashtable的使用nHashtable的常用屬性見下表n Hashtable 的 key 和 value 都是 object 類型的,所以在訪問時(shí)必須從 object 類型轉(zhuǎn)換回存儲(chǔ)前的類型。屬性功能說明Keys獲取包含 Hashtable 中所有鍵的 ICollectionValues獲取包含 Hashtable 中所有值的 ICollectionCount獲取 Hashtable 中鍵值對(duì)的數(shù)目3.3.2 Hashtable的使用nC# 支持 foreach 關(guān)鍵字,foreach 循環(huán)在處理集合和數(shù)
22、組相對(duì)于 for 循環(huán)存在以下 4 個(gè)優(yōu)勢:nforeach 語句簡潔nforeach 循環(huán)效率比 for 循環(huán)要高n不用關(guān)心數(shù)組的起始索引是什么n處理多維數(shù)組更加方便foreach(Typetinarrays) /.doyourthingsType: 需要遍歷每一個(gè)對(duì)象的類型。需要遍歷每一個(gè)對(duì)象的類型。t: 每一個(gè)對(duì)象。每一個(gè)對(duì)象。arrays: 需要遍歷的對(duì)象。需要遍歷的對(duì)象。3.3.2 Hashtable的使用n使用 foreach 循環(huán)遍歷 Hashtablestatic void Main(string args) /初始化Hashtable語句省略 Console.WriteLin
23、e(list 包含以下鍵:); foreach (object key in list.Keys) Console.WriteLine(key); Console.WriteLine(list 包含以下值:); foreach (object value in list.Values) Customer cust = value as Customer;Console.WriteLine( 姓名:0,年齡:1,住址:2, cust.Name, cust.Age, cust.Address); Console.WriteLine(list 中鍵值對(duì)的個(gè)數(shù):0, list.Count);遍歷 Ha
24、shtable 中所有鍵對(duì)象遍歷 Hashtable 中所有值對(duì)象3.3.2 HashTable的使用nHashtable的常用方法方法功能說明Add(objectkey,objectvalue)將帶有指定鍵和值的元素添加到 Hashtable 中Remove(objectkey) 從 Hashtable 中移除帶有指定鍵的元素Clear()移除 Hashtable 中所有元素ContainsKey(objectkey) 確定 Hashtable 中是否包含指定鍵ContainsValue(objectvalue) 確定 Hashtable 中是否包含指定值3.3.2 Hashtable的使用
25、static void Main(string args) Customer jun = new Customer( 李軍 ,32, 廣州 ); Customer yun = new Customer( 王云 , 28, 杭州 ); Customer si = new Customer( 雷斯 , 43, 美國 ); Hashtable list = new Hashtable(); list.Add(jun.Name, jun); list.Add(yun.Name, yun); list.Add(si.Name, si); list.Remove(yun.Name); foreach (o
26、bject value in list.Values) Customer cust = value as Customer;Console.WriteLine( 姓名:0,年齡:1,住址:2, cust.Name, cust.Age, cust.Address); 通過 key 來刪除集合中的元素n刪除Hashtable中的元素3.3.2 Hashtable的使用static void Main(string args) /初始化Hashtable語句省略 string name = 李宏 ; if (list.ContainsKey(name) Customer cust = listnam
27、e as Customer;Console.WriteLine(0 客戶在排隊(duì),其住址是:1, name, cust.Address); else Console.WriteLine(0 客戶沒有排隊(duì)。, name); 先判斷是否有鍵為 name 的元素n訪問Hashtable中的指定元素需求說明實(shí)踐時(shí)間: 分鐘3.3.3 學(xué)生實(shí)踐練習(xí)n 在實(shí)際工作中,經(jīng)常涉及到對(duì)容器中的數(shù)據(jù)進(jìn)行操作,本案例將使用 Hashtable 實(shí)現(xiàn)對(duì)三國時(shí)期人物的添加、移除、判斷是否存在和清除等操作25實(shí)現(xiàn)思路3.3.3 學(xué)生實(shí)踐練習(xí)n 創(chuàng)建 Windows 窗體應(yīng)用,取名為 ThreeKingdomsHashtablen 添加 Windows 窗體,命名為 Operation,并在該窗體中添加 btnAdd(添加)、btnRemove(移除)、btnContainsKey(判斷是否包含)和 btnClear(清除)等按鈕對(duì)容器進(jìn)行操作,添加 txtInfo(顯示文本框)顯n 定義一個(gè) Hashtable 方法集合用于存儲(chǔ)三國人物,并實(shí)現(xiàn)點(diǎn)擊相應(yīng)的按鈕對(duì) Hashtable 中的三國人物做出相應(yīng)的操作,并重新顯示三國人物信息n
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 在線教育平臺(tái)內(nèi)容制作手冊(cè)
- 房屋買賣合同居間協(xié)議
- 工程管理質(zhì)量與安全控制手冊(cè)
- 家具廠廠長聘任書合同
- 地皮交易居間協(xié)議合同
- 2025年綿陽貨運(yùn)從業(yè)資格證考試題庫
- 《數(shù)據(jù)可視化技術(shù)應(yīng)用》3.3 構(gòu)建銷售數(shù)據(jù)動(dòng)態(tài)分析看板-教案
- 員工上下班安全協(xié)議書5篇
- 廠房消防勞務(wù)承包合同范例
- 淮北房產(chǎn)合同范本
- 第四屆博德世達(dá)杯全國石油工程知識(shí)競賽樣題及答案模板
- 西北工業(yè)大學(xué)英文簡介
- 《動(dòng)畫場景設(shè)計(jì)》第一章 動(dòng)畫場景設(shè)計(jì)概述
- 2023年湖北宜昌伍家新城投資控股集團(tuán)有限公司招聘筆試題庫含答案解析
- 內(nèi)燃平衡重式叉車
- 品質(zhì)管理人員角色定位
- 專業(yè)形體訓(xùn)練項(xiàng)目課程標(biāo)準(zhǔn)
- 人保理賠員試題車險(xiǎn)查勘定損
- 小學(xué)生寒假生活成長冊(cè)PPT
- GB/T 25775-2010焊接材料供貨技術(shù)條件產(chǎn)品類型、尺寸、公差和標(biāo)志
- 10KV開關(guān)柜教學(xué)講解課件
評(píng)論
0/150
提交評(píng)論