實驗05集合類與泛型實驗報告_第1頁
實驗05集合類與泛型實驗報告_第2頁
實驗05集合類與泛型實驗報告_第3頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論