Java generics - - PowerPoint PPT Presentation

java generics
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Java ¡generics ¡

CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡ ¡

h"p://www.flickr.com/photos/pdxdiver/4917853457/ ¡ h"p://www.flickr.com/photos/paj/4002324674/ ¡

slide-2
SLIDE 2

Overview ¡

2 ¡

  • 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 ¡

slide-3
SLIDE 3

Stack ¡that ¡holds ¡Strings ¡

3 ¡ public ¡class ¡StackOfStrings ¡ { ¡ ¡ ¡ ¡ ¡private ¡class ¡Node ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡private ¡String ¡item; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡private ¡Node ¡next; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡private ¡Node ¡first ¡= ¡null; ¡ ¡ ¡ ¡ ¡ ¡public ¡boolean ¡isEmpty() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡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; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ... ¡

  • Implemented ¡as ¡a ¡linked ¡list, ¡but ¡clients ¡
  • f ¡StackOfStrings ¡don't ¡need ¡to ¡know ¡

the ¡implementaKon ¡details ¡

  • Clients ¡may ¡care ¡about ¡performance ¡

guarantees: ¡

ü push() ¡takes ¡constant ¡Kme ¡ ü pop() ¡takes ¡constant ¡Kme ¡

slide-4
SLIDE 4

Stack ¡that ¡holds ¡doubles ¡

4 ¡ 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; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ... ¡

slide-5
SLIDE 5

Stack ¡that ¡holds ¡Gems ¡

5 ¡ 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) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Node ¡node ¡= ¡new ¡Node(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.item ¡= ¡s; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.next ¡= ¡first; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡node; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡public ¡Gem ¡pop() ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(first ¡== ¡null) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡throw ¡new ¡RuntimeException("Stack ¡is ¡empty!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Gem ¡result ¡= ¡first.item; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡first.next; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡result; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ... ¡

Repeated ¡code ¡is ¡evil! ¡ ¡

(also ¡prevents ¡development ¡of ¡ generally ¡applicable ¡container ¡ class ¡such ¡as ¡ArrayList) ¡

slide-6
SLIDE 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 ¡

6 ¡

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. ¡

slide-7
SLIDE 7

Reversing ¡numbers ¡in ¡a ¡file: ¡failure ¡

7 ¡

import ¡java.util.ArrayList; ¡ ¡ public ¡class ¡ReverseNums ¡ { ¡ ¡public ¡static ¡void ¡main(String[] ¡args) ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ArrayList<double> ¡nums ¡= ¡new ¡ArrayList<double>(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(!StdIn.isEmpty()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡nums.add(StdIn.readDouble()); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡nums.size() ¡-­‑ ¡1; ¡i ¡>= ¡0; ¡i-­‑-­‑) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println(nums.get(i)); ¡ ¡ ¡ ¡} ¡ } ¡

This ¡will ¡not ¡work! ¡ ¡ Java ¡generics ¡like ¡

ArrayList ¡only ¡

take ¡reference ¡data ¡ types, ¡not ¡primiKve ¡ types ¡like ¡double. ¡

slide-8
SLIDE 8

Using ¡primiKve ¡wrapper ¡classes: ¡success ¡

8 ¡

import ¡java.util.ArrayList; ¡ ¡ public ¡class ¡ReverseNums ¡ { ¡ ¡public ¡static ¡void ¡main(String[] ¡args) ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ArrayList<Double> ¡nums ¡= ¡new ¡ArrayList<Double>(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(!StdIn.isEmpty()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡nums.add(StdIn.readDouble()); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡nums.size() ¡-­‑ ¡1; ¡i ¡>= ¡0; ¡i-­‑-­‑) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println(nums.get(i)); ¡ ¡ ¡ ¡} ¡ } ¡

Double ¡class ¡wraps ¡

a ¡primiKve ¡double ¡ data ¡type ¡into ¡an ¡

  • bject ¡so ¡we ¡can ¡

put ¡it ¡into ¡the ¡

  • ArrayList. ¡
slide-9
SLIDE 9

Java ¡primiKve ¡wrapper ¡classes ¡

9 ¡

Primi5ve ¡type ¡ Wrapper ¡class ¡ byte ¡ Byte ¡ short ¡ Short ¡ int ¡ Integer ¡ long ¡ Long ¡ float ¡ Float ¡ double ¡ Double ¡ char ¡ Character ¡ boolean ¡ Boolean ¡

  • Wrapper ¡classes ¡

– Provide ¡a ¡way ¡to ¡use ¡ primiKves ¡types ¡with ¡ generics ¡like ¡ArrayList ¡ – Usually ¡primiKve ¡type ¡ capitalized ¡ – But: ¡ ¡

  • SKck ¡to ¡primiKves ¡unless ¡a ¡

wrapper ¡is ¡required ¡ ¡

  • Wrappers ¡have ¡overheads, ¡

e.g. ¡object ¡creaKon, ¡garbage ¡ collecKon ¡

slide-10
SLIDE 10

Unboxing: ¡ This ¡works ¡even ¡though ¡the ¡right ¡side ¡ needs ¡a ¡primiKve ¡double. ¡ ¡Java ¡converts ¡ from ¡Double ¡to ¡a ¡primiKve ¡double. ¡ ¡

Wrapper ¡automagic ¡

  • Autoboxing ¡/ ¡Unboxing ¡

– Java ¡5.0 ¡automagically ¡converts ¡to/from ¡ wrapper ¡as ¡needed ¡ – Example: ¡storing ¡and ¡summing ¡doubles ¡in ¡a ¡file ¡

10 ¡

ArrayList<Double> ¡nums ¡= ¡new ¡ArrayList<Double>(); ¡ ¡ ¡ ¡ while ¡(!StdIn.isEmpty()) ¡ ¡ ¡ ¡nums.add(StdIn.readDouble()); ¡ ¡ ¡ ¡ double ¡sum ¡= ¡0.0; ¡ for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡nums.size(); ¡i++) ¡ ¡ ¡ ¡sum ¡+= ¡nums.get(i); ¡ ¡ System.out.println("sum ¡= ¡" ¡+ ¡sum); ¡ ¡ ¡

Autoboxing: ¡ This ¡works ¡even ¡though ¡

StdIn.readDouble()

returns ¡a ¡primiKve ¡

double ¡but ¡the ¡ ArrayList ¡requires ¡a ¡ Double ¡object. ¡

Integer ¡ int ¡

slide-11
SLIDE 11

ThreeSum ¡using ¡int ¡primiKve ¡

11 ¡

public ¡class ¡ThreeSum ¡ ¡ { ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡[] ¡nums ¡= ¡new ¡int[N]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡nums[i] ¡= ¡(int) ¡(Math.random() ¡* ¡2000) ¡-­‑ ¡1000; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Stats ¡stats ¡= ¡new ¡Stats(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Now ¡try ¡and ¡find ¡triples ¡that ¡sum ¡to ¡0 ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡j ¡= ¡i ¡+ ¡1; ¡j ¡< ¡N; ¡j++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡k ¡= ¡j ¡+ ¡1; ¡k ¡< ¡N; ¡k++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡theSum ¡= ¡nums[i] ¡+ ¡nums[j] ¡+ ¡nums[k]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(theSum ¡== ¡0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/* ¡removed ¡for ¡timing ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("3-­‑sum ¡for ¡N ¡= ¡" ¡+ ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println(stats); ¡ ¡ ¡ ¡} ¡ } ¡

% ¡java ¡ThreeSum ¡3000 ¡ 3-­‑sum ¡for ¡N ¡= ¡3000 ¡ elapsed ¡time ¡(s) ¡ ¡ ¡ ¡ ¡ ¡: ¡ ¡0.0300 ¡ heap ¡memory ¡used ¡(MB) ¡: ¡ ¡0.7631 ¡ max ¡memory ¡(MB) ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡1820.5000 ¡

slide-12
SLIDE 12

ThreeSum ¡using ¡Integer ¡wrapper ¡

12 ¡

public ¡class ¡ThreeSumWrapper ¡ ¡ { ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡[] ¡nums ¡= ¡new ¡Integer ¡[N]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(Integer ¡i ¡= ¡0; ¡i ¡< ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡nums[i] ¡= ¡(int) ¡(Math.random() ¡* ¡2000) ¡-­‑ ¡1000; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Stats ¡stats ¡= ¡new ¡Stats(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Now ¡try ¡and ¡find ¡triples ¡that ¡sum ¡to ¡0 ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(Integer ¡i ¡= ¡0; ¡i ¡< ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(Integer ¡j ¡= ¡i ¡+ ¡1; ¡j ¡< ¡N; ¡j++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(Integer ¡k ¡= ¡j ¡+ ¡1; ¡k ¡< ¡N; ¡k++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡theSum ¡= ¡nums[i] ¡+ ¡nums[j] ¡+ ¡nums[k]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(theSum ¡== ¡0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/* ¡removed ¡for ¡timing ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("3-­‑sum ¡for ¡N ¡= ¡" ¡+ ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println(stats); ¡ ¡ ¡ ¡} ¡ } ¡

% ¡java ¡ThreeSumWrapper ¡3000 ¡ 3-­‑sum ¡for ¡N ¡= ¡3000 ¡ elapsed ¡time ¡(s) ¡ ¡ ¡ ¡ ¡ ¡: ¡46.3760 ¡ heap ¡memory ¡used ¡(MB) ¡: ¡89.1215 ¡ max ¡memory ¡(MB) ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡1820.5000 ¡

slide-13
SLIDE 13

Generic ¡Stack ¡class ¡

13 ¡

public ¡class ¡Stack<E> ¡ { ¡ ¡ ¡ ¡ ¡private ¡class ¡Node ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡private ¡E ¡ ¡ ¡ ¡item; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡private ¡Node ¡next; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡private ¡Node ¡first ¡= ¡null; ¡ ¡ ¡ ¡ ¡ ¡public ¡void ¡push(E ¡s) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Node ¡node ¡= ¡new ¡Node(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.item ¡= ¡s; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.next ¡= ¡first; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡node; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡

¡

E ¡is ¡for ¡Element, ¡the ¡type ¡of ¡the ¡elements ¡ stored ¡by ¡the ¡stack. ¡ ¡Replaces ¡all ¡occurrences ¡of ¡

String ¡in ¡StackOfStrings. ¡

¡

We ¡now ¡have ¡a ¡class ¡that ¡can ¡store ¡any ¡ (reference) ¡data ¡type. ¡

h"p://www.flickr.com/photos/mirka23/4580726172/ ¡ h"p://www.flickr.com/photos/tavallai/4536294812/ ¡

slide-14
SLIDE 14

Generic ¡Stack ¡class ¡

14 ¡

public ¡class ¡Stack<Item> ¡ { ¡ ¡ ¡ ¡ ¡private ¡class ¡Node ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡private ¡Item ¡item; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡private ¡Node ¡next; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡private ¡Node ¡first ¡= ¡null; ¡ ¡ ¡ ¡ ¡ ¡public ¡void ¡push(Item ¡s) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Node ¡node ¡= ¡new ¡Node(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.item ¡= ¡s; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡node.next ¡= ¡first; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first ¡= ¡node; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡

¡

E ¡is ¡for ¡Element, ¡but ¡nothing ¡special ¡about ¡E. ¡ Any ¡Java ¡idenKfier ¡can ¡be ¡used ¡such ¡as ¡Item. ¡ ¡ ¡ It ¡just ¡needs ¡to ¡be ¡used ¡consistently ¡in ¡the ¡ declaraKon ¡of ¡the ¡generic ¡class. ¡

h"p://www.flickr.com/photos/mirka23/4580726172/ ¡ h"p://www.flickr.com/photos/tavallai/4536294812/ ¡

slide-15
SLIDE 15

Example ¡of ¡using ¡the ¡generic ¡Stack ¡

15 ¡

Stack<String> ¡s1 ¡= ¡new ¡Stack<String>(); ¡ s1.push("it"); ¡ s1.push("was"); ¡ s1.push("the"); ¡ s1.push("best"); ¡ s1.push("of"); ¡ s1.push("times"); ¡ while ¡(!s1.isEmpty()) ¡ ¡ ¡ ¡System.out.println("S1 ¡popped: ¡" ¡+ ¡s1.pop() ¡+ ¡", ¡stack ¡= ¡" ¡+ ¡s1); ¡

The ¡type ¡String ¡gets ¡subsKtuted ¡for ¡the ¡ "wildcard" ¡type ¡in ¡the ¡generic ¡Stack ¡class. ¡

¡

The ¡object ¡s1 ¡can ¡hold ¡anything ¡of ¡type ¡String ¡ (or ¡any ¡subclass, ¡but ¡since ¡the ¡String ¡class ¡is ¡ declared ¡final ¡there ¡are ¡no ¡subclasses) ¡

slide-16
SLIDE 16

Programming ¡acKvity ¡

  • Goal: ¡Finish ¡the ¡generic ¡Stack ¡class ¡

– Team ¡1: ¡pop() ¡ – Team ¡2: ¡elementAt() ¡ – Team ¡3: ¡indexOf() ¡ – Team ¡4: ¡size() ¡

  • Send ¡a ¡representaKve ¡to ¡enter ¡your ¡code ¡

– Used ¡in ¡my ¡Towers ¡of ¡Hanoi ¡game: ¡

¡

16 ¡

¡ Stack<Disk> ¡peg1 ¡= ¡new ¡Stack<Disk>(); ¡ ¡ Stack<Disk> ¡peg2 ¡= ¡new ¡Stack<Disk>(); ¡ ¡ Stack<Disk> ¡peg3 ¡= ¡new ¡Stack<Disk>(); ¡ ¡

peg1 peg2 peg3 Replace ¡all ¡TODO ¡with ¡some ¡code ¡

slide-17
SLIDE 17

Summary ¡

  • Java ¡generics ¡

– Create ¡one ¡class ¡that ¡can ¡store ¡any ¡object ¡type ¡ – Replace ¡real ¡type ¡with ¡"wildcard" ¡in ¡class ¡blueprint ¡ – Generics ¡use ¡the ¡<> ¡bracket ¡notaKon ¡ – PrimiKves ¡need ¡to ¡use ¡wrapper ¡class ¡

  • Integer ¡instead ¡of ¡int ¡
  • Double ¡instead ¡of ¡double ¡
  • … ¡

17 ¡