![實驗05集合類與泛型實驗報告_第1頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/31/8c55b072-0c12-4ef2-bae3-f4316eee97ed/8c55b072-0c12-4ef2-bae3-f4316eee97ed1.gif)
![實驗05集合類與泛型實驗報告_第2頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/31/8c55b072-0c12-4ef2-bae3-f4316eee97ed/8c55b072-0c12-4ef2-bae3-f4316eee97ed2.gif)
![實驗05集合類與泛型實驗報告_第3頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/31/8c55b072-0c12-4ef2-bae3-f4316eee97ed/8c55b072-0c12-4ef2-bae3-f4316eee97ed3.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實驗05集合類與泛型 實驗報告實驗五集合類與泛型1.實驗?zāi)康模?)掌握ArrayList類與LinkedList類的用法:(2)掌握TreeSet類的用法;(3)掌握Map接口及其實現(xiàn)類的用法(4)掌握泛型的用法。2.實驗內(nèi)容實驗題 1 有四個類,主類 Store 在包 on. edu. nwsuaf. jp. p4 中,Mobilen Mp3Playern Product 在包 cn. edu. nwsuaf. jp. p4. data 中,Mobile、Mp3Player 是 Product 的子類, Product 類實現(xiàn) Comparable 接 口,重寫 / Comparable 接
2、 口中方法 compareTo* 實現(xiàn)了 product對象按照價格排序?;疽螅海?)在主類Store中實例化多個Mobile類與1p3Player的實例,分別 將這些實例用ArrayList與LinkedList存放,最后用StringBuiIder存放并將其輸出。(2)用迭代器(iterator)將實例對象輸出(要求用加強型for循環(huán))。實驗結(jié)果:代碼:Store類:package cn. edu. nwsuaf. jp. p4; import java. util. ArrayList; import java. util. Arrays; import java. util. It
3、erator; importcn. edu. nwsuaf. jp. p4. data. Mobile; import cn. edu. nwsuaf. jp. p4. data. Mp3Player; import cn. edu. nwsuaf. jp. p4. data. Product; public class Store public static void main(String 1 args) Mp3Player pl = new Mp3Player(, 399.Of); Mp3Player p2 = new Mp3Player(, 580. Of); Mp3Player p3
4、 = new Mp3Player(XM Mp3Play(256MB), 930. Of); Mobile ml = new Mobile(, 1780. Of); Mobile m2 =new Mobile(, 1450.Of); Product products = pl, p2, p3, ml, m2 ; Arrays, sort(products); ArrayList list = new ArrayList(); for (int i = 0; i < products, length; i+) list, add(productsLi);/ 用 StringBuiIder 輸
5、出 StringBuilder text = new StringBuilder 0 ; for (Product product : products) text, append(product); text, append(); System, out. printin(ffl StringBuilder ) ; System, out. printin( + + text + + Product. getCount 0 + ) ; / 用迭代器輸出 Iterator it = list. iterator ();System, out. printin(、用迭代器輸出:) ; Syste
6、m. out. printin() ; while (it. hasNext 0) System, out. printin (it. next () ; System, out. println(are + Product. getCount 0 + products. ); Product 類:package cn. edu. nwsuaf. jp. p4. data; SuppressWarnings() public abstract class Product implements Comparable private String Name; private Float price
7、; public static int count = 0; public Product (String name, Float price) super(); Name = name; this.price = price; +count;public String getName() return Name; public void setName(String name) Name = name; public Float getPrice() return price; public void setPrice (Float price) this, price = price; p
8、ublic static int getCount() return count; public static void setCount(int count) Product, count = count; ©Override public String toStringO return getName() + + getPrice 0 + ; public abstract voidoutput(Product p); ©Override public int compareTo(Object o) / TODOAuto-generated method stub Pr
9、oduct product = (Product) o; return new Float(getPrice0). compareTo(product. getPrice0) ; public void sell(int i)/ TODO Auto-generated method stub )Mp3Playert 類:public class Mp3Player extends Product public Mp3Player(String name, Float price) super(name, price); ©Override public voidoutput (Pro
10、duct p) System, out. println( Mp3Player. class. toStringO) ;Mobile 類:package cn. edu. nwsuaf. jp. p4. data; public class Mobile extends Product public Mobile(String name, Float price) super(name, price);©Override public void output(Product p) System. out. printin( Mobile. class. toStringO) ; 實驗
11、題 2 項目包含類見實驗題lo基本要求:(1)在主類Store中實例化多個Mobile類與Mp3Player 的實例,分別將這些實例用TreeSet存放,最后用StringBuiler存放并將其輸出。(2)用迭代器(iterator)將實例對象輸出。實驗結(jié)果:package cn. edu. nwsuaf. jp. p4. data;代碼:基本類同上,只需修改Store類即可public class Store public static void main(StringEl args) Mp3Player pl = new Mp3Player( Mp3Player p2 =new Mp3Pl
12、ayer( Mp3Player p3 = new Mp3Player( Mobile ml = new Mobile( Mobile m2 = new Mobile(Productproducts = pl, p2, p3, ml, m2 ;Arrays, sort(products); TreeSet mytree = new TreeSet (); for (int i = 0; i < products, length; i+) mytree. add(productsEi); StringBuildertext = new StringBuilder(); for (Product product : products) text, append (product) ; text, append ( System, out. printin (、用StringBuilder 輸出: System,
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度農(nóng)業(yè)合作社季節(jié)性工人雇傭合同
- 2025年中國通信導(dǎo)航行業(yè)市場全景分析及投資前景展望報告
- 2025年度住宅建筑施工合同節(jié)能減排示范文本
- 給村委會申請書范文
- 中級職稱申請書范文
- 幼兒園三兒資助申請書
- 2025年度人工智能教育平臺建設(shè)合同范本
- 電動汽車電機的智能化控制策略實踐
- 2025年高級電腦肌電儀項目投資可行性研究分析報告
- 2025年度智能設(shè)備固定資產(chǎn)借貸合同范本
- AQ6111-2023個體防護裝備安全管理規(guī)范
- (正式版)JBT 9229-2024 剪叉式升降工作平臺
- 中國紅十字會救護員培訓(xùn)理論考試試題及答案
- 兒童體液平衡及液體療法課件
- 2023版押品考試題庫必考點含答案
- 昆明天大礦業(yè)有限公司尋甸縣金源磷礦老廠箐-小凹子礦段(擬設(shè))采礦權(quán)出讓收益評估報告
- 心有榜樣行有力量 -從冬奧冠軍徐夢桃身上感受青春奮斗初中主題班會
- GB/T 3860-1995文獻敘詞標(biāo)引規(guī)則
- 最新《工會基礎(chǔ)知識》試題庫及答案1000題【完美打印版】
- 七年級英語下冊閱讀理解10篇
- 設(shè)計質(zhì)量、進度保證措施
評論
0/150
提交評論