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/ ¡
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
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/ ¡
2 ¡
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; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ... ¡
ü push() ¡takes ¡constant ¡Kme ¡ ü pop() ¡takes ¡constant ¡Kme ¡
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; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ... ¡
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; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ... ¡
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)); ¡ ¡ ¡
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)); ¡ ¡ ¡ ¡} ¡ } ¡
ArrayList ¡only ¡
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 ¡
9 ¡
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); ¡ ¡ ¡
StdIn.readDouble()
double ¡but ¡the ¡ ArrayList ¡requires ¡a ¡ Double ¡object. ¡
Integer ¡ int ¡
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 ¡
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 ¡
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; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡
¡
String ¡in ¡StackOfStrings. ¡
¡
h"p://www.flickr.com/photos/mirka23/4580726172/ ¡ h"p://www.flickr.com/photos/tavallai/4536294812/ ¡
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; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡
¡
h"p://www.flickr.com/photos/mirka23/4580726172/ ¡ h"p://www.flickr.com/photos/tavallai/4536294812/ ¡
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); ¡
¡
16 ¡
¡ Stack<Disk> ¡peg1 ¡= ¡new ¡Stack<Disk>(); ¡ ¡ Stack<Disk> ¡peg2 ¡= ¡new ¡Stack<Disk>(); ¡ ¡ Stack<Disk> ¡peg3 ¡= ¡new ¡Stack<Disk>(); ¡ ¡
17 ¡