java generics
play

Java generics - PowerPoint PPT Presentation

Java generics h"p://www.flickr.com/photos/pdxdiver/4917853457/ h"p://www.flickr.com/photos/paj/4002324674/ CSCI 136: Fundamentals of Computer Science II Keith Vertanen


  1. Java ¡generics ¡ h"p://www.flickr.com/photos/pdxdiver/4917853457/ ¡ h"p://www.flickr.com/photos/paj/4002324674/ ¡ CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡ ¡

  2. Overview ¡ • Abstract ¡Data ¡Types ¡(ADTs) ¡ – A ¡collecKon ¡of ¡data ¡and ¡operaKons ¡on ¡that ¡data ¡ – Implemented ¡by ¡some ¡underlying ¡data ¡structure ¡ • Thus ¡far: ¡ ¡ – Class ¡depends ¡on ¡data ¡type ¡we ¡wanted ¡to ¡store ¡ • StackOfStrings, ¡GemList, ¡StackOfIntegers, ¡ StackOfDoubles, ¡StackOfEnemies, ¡ StackOfStackOfStrings, ¡… ¡ • Today: ¡Generics ¡ – Roll ¡your ¡own ¡mulK-­‑purpose ¡collecKons ¡ – Helps ¡you ¡understand ¡Java's ¡<> ¡notaKon ¡ 2 ¡

  3. Stack ¡that ¡holds ¡Strings ¡ public ¡class ¡StackOfStrings ¡ • Implemented ¡as ¡a ¡linked ¡list, ¡but ¡clients ¡ { ¡ ¡ ¡ ¡ ¡ private ¡class ¡Node ¡ of ¡ StackOfStrings ¡don't ¡need ¡to ¡know ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ private ¡String ¡item; ¡ ¡ ¡ the ¡implementaKon ¡details ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ private ¡Node ¡next; ¡ • Clients ¡may ¡care ¡about ¡performance ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ private ¡Node ¡first ¡= ¡ null ; ¡ guarantees: ¡ ¡ ¡ ¡ ¡ ¡public ¡boolean ¡ isEmpty() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ü push() ¡takes ¡constant ¡Kme ¡ ¡ ¡ ¡ ¡{ ¡ ü pop() ¡takes ¡constant ¡Kme ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡(first ¡== ¡ null ); ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ public ¡void ¡ push(String ¡s) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Node ¡node ¡= ¡ new ¡ Node(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.item ¡= ¡s; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.next ¡= ¡first; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡node; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ public ¡String ¡pop() ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ¡(first ¡== ¡ null ) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡throw ¡new ¡ RuntimeException("Stack ¡is ¡empty!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡result ¡= ¡first.item; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡first.next; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡ result; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ... ¡ 3 ¡

  4. Stack ¡that ¡holds ¡doubles ¡ public ¡class ¡StackOfDoubles ¡ { ¡ ¡ ¡ ¡ ¡ private ¡class ¡Node ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ private ¡ double ¡item; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ private ¡Node ¡next; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ private ¡Node ¡first ¡= ¡ null ; ¡ ¡ ¡ ¡ ¡ ¡public ¡boolean ¡ isEmpty() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡(first ¡== ¡ null ); ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ public ¡void ¡ push( double ¡s) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Node ¡node ¡= ¡ new ¡ Node(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.item ¡= ¡s; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.next ¡= ¡first; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡node; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ public ¡ double ¡pop() ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ¡(first ¡== ¡ null ) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡throw ¡new ¡ RuntimeException("Stack ¡is ¡empty!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ double ¡result ¡= ¡first.item; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡first.next; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡ result; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ... ¡ 4 ¡

  5. Stack ¡that ¡holds ¡Gems ¡ public ¡class ¡StackOfGems ¡ { ¡ ¡ ¡ ¡ ¡ private ¡class ¡Node ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ private ¡Gem ¡item; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ private ¡Node ¡next; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ private ¡Node ¡first ¡= ¡ null ; ¡ ¡ ¡ ¡ ¡ ¡public ¡boolean ¡ isEmpty() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡(first ¡== ¡ null ); ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ public ¡void ¡ push(Gem ¡s) ¡ ¡ ¡ Repeated ¡code ¡is ¡evil! ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Node ¡node ¡= ¡ new ¡ Node(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.item ¡= ¡s; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.next ¡= ¡first; ¡ (also ¡prevents ¡development ¡of ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡node; ¡ generally ¡applicable ¡container ¡ ¡ ¡ ¡ ¡} ¡ ¡ class ¡such ¡as ¡ ArrayList ) ¡ ¡ ¡ ¡ ¡ public ¡Gem ¡pop() ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ¡(first ¡== ¡ null ) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡throw ¡new ¡ RuntimeException("Stack ¡is ¡empty!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Gem ¡result ¡= ¡first.item; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡first.next; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡ result; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ... ¡ 5 ¡

  6. Java ¡generics ¡ • Java ¡generics ¡ – Added ¡in ¡Java ¡5.0 ¡ – Create ¡class ¡that ¡can ¡hold ¡any ¡(reference) ¡type ¡ • As ¡usual: ¡primiKve ¡types ¡not ¡allowed ¡ – Must ¡use ¡wrappers ¡like ¡ Integer ¡and ¡ Double ¡ – We've ¡seen ¡this ¡before: ¡ ArrayList ¡ • Thing ¡inside ¡ <> 's ¡is ¡the ¡type ¡used ¡by ¡generic ¡collecKon ¡ ArrayList<String> ¡lines ¡= ¡ new ¡ArrayList<String>(); ¡ ¡ ¡ ¡ while ¡(!StdIn. isEmpty() ) ¡ ¡ ¡ ¡ ¡lines.add(StdIn. readLine() ); ¡ ¡ ¡ ¡ ¡ for ¡( int ¡i ¡= ¡lines.size() ¡-­‑ ¡1; ¡i ¡>= ¡0; ¡i-­‑-­‑) ¡ ¡ ¡ ¡ ¡System. out .println(lines.get(i)); ¡ ¡ ¡ Reverses ¡the ¡lines ¡in ¡a ¡file. ¡ 6 ¡

  7. Reversing ¡numbers ¡in ¡a ¡file: ¡failure ¡ import ¡java.util.ArrayList; ¡ This ¡will ¡not ¡work! ¡ ¡ ¡ public ¡class ¡ ReverseNums ¡ Java ¡generics ¡like ¡ { ¡ ArrayList ¡only ¡ ¡ public ¡static ¡void ¡ main(String[] ¡args) ¡ take ¡reference ¡data ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ArrayList< double > ¡nums ¡= ¡ new ¡ArrayList< double >(); ¡ types, ¡not ¡primiKve ¡ ¡ ¡ ¡ types ¡like ¡ double . ¡ ¡ ¡ ¡ ¡ ¡ ¡ while ¡(!StdIn.isEmpty()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡nums.add(StdIn.readDouble()); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ for ¡( int ¡i ¡= ¡nums.size() ¡-­‑ ¡1; ¡i ¡>= ¡0; ¡i-­‑-­‑) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System. out .println(nums.get(i)); ¡ ¡ ¡ ¡} ¡ } ¡ 7 ¡

  8. Using ¡primiKve ¡wrapper ¡classes: ¡success ¡ import ¡java.util.ArrayList; ¡ Double ¡class ¡wraps ¡ ¡ public ¡class ¡ ReverseNums ¡ a ¡primiKve ¡ double ¡ { ¡ data ¡type ¡into ¡an ¡ ¡ public ¡static ¡void ¡ main(String[] ¡args) ¡ object ¡so ¡we ¡can ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ArrayList<Double> ¡nums ¡= ¡ new ¡ArrayList<Double>(); ¡ put ¡it ¡into ¡the ¡ ¡ ¡ ¡ ArrayList . ¡ ¡ ¡ ¡ ¡ ¡ ¡ while ¡(!StdIn.isEmpty()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡nums.add(StdIn.readDouble()); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ for ¡( int ¡i ¡= ¡nums.size() ¡-­‑ ¡1; ¡i ¡>= ¡0; ¡i-­‑-­‑) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System. out .println(nums.get(i)); ¡ ¡ ¡ ¡} ¡ } ¡ 8 ¡

  9. Java ¡primiKve ¡wrapper ¡classes ¡ • Wrapper ¡classes ¡ – Provide ¡a ¡way ¡to ¡use ¡ Primi5ve ¡type ¡ Wrapper ¡class ¡ primiKves ¡types ¡with ¡ byte ¡ Byte ¡ generics ¡like ¡ ArrayList ¡ short ¡ Short ¡ – Usually ¡primiKve ¡type ¡ int ¡ Integer ¡ long ¡ Long ¡ capitalized ¡ float ¡ Float ¡ – But: ¡ ¡ double ¡ Double ¡ • SKck ¡to ¡primiKves ¡unless ¡a ¡ char ¡ Character ¡ wrapper ¡is ¡required ¡ ¡ boolean ¡ Boolean ¡ • Wrappers ¡have ¡overheads, ¡ e.g. ¡object ¡creaKon, ¡garbage ¡ collecKon ¡ 9 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend