250 likes | 559 Views
Genericity. collections of objects. collections of objects. for real programming applications, pratical collections are collections of objects example: collection of BankAccounts for reusability, collections are collections of Object s, used with typecasting
E N D
Genericity collections of objects
collections of objects • for real programming applications, pratical collections are collections of objects • example: collection of BankAccounts • for reusability, collections are collections of Objects, used with typecasting • collections of Objects can hold objects of different classes COSC 2006 Bags of Objects
rewriting collections for objects • typecasting ‘in’ and ‘out’ • comparing objects including null • removing objects from arrays • clone-ing • primitives in collections: wrappers • iterators COSC 2006 Bags of Objects
bags of Objects • implemented with array • ArrayBag.java • implemented with linked list • LinkedBag.java COSC 2006 Bags of Objects
Example: ArrayBag.java • bag of Objects implemented with array containing 4 String objects int manyItems 4 Object[] data “blue” “red” “blue” “green” COSC 2006 Bags of Objects
ArrayBag constructor • ArrayBag a = new ArrayBag(5); a int manyItems 0 Object[] data COSC 2006 Bags of Objects
ArrayBag constructor • ArrayBag bag = new ArrayBag(); bag int manyItems 0 Object[] data COSC 2006 Bags of Objects
add method • bag.add(“orange”); bag 5 int manyItems 4 Object[] data “green” “blue” “red” “blue” “orange” COSC 2006 Bags of Objects
add method – null object • bag.add(null); bag 5 int manyItems 4 Object[] data “green” “blue” “red” “blue” COSC 2006 Bags of Objects
countOccurrences • int c = bag.countOccurrences(“blue”); target bag “blue” int manyItems 4 Object[] data “green” “blue” “red” “blue” COSC 2006 Bags of Objects
countOccurrences – null target • int c = bag.countOccurrences(null) target bag int manyItems 4 Object[] data “green” “blue” “blue” COSC 2006 Bags of Objects
remove method • boolean r = bag.remove(“blue”); bag target 3 int manyItems “blue” 4 Object[] data “blue” “red” “blue” “green” COSC 2006 Bags of Objects
remove method – null target • boolean r = bag.remove(null); bag target 3 int manyItems 4 Object[] data “green” “blue” “blue” COSC 2006 Bags of Objects
returning an object • Object o = bag.grab(); • String s = (String) o; bag int manyItems 4 Object[] data Object o String s “blue” “red” “blue” “green” COSC 2006 Bags of Objects
returning an object • String s = (String) bag.grab(); bag int manyItems 4 Object[] data String s “blue” “red” “blue” “green” COSC 2006 Bags of Objects
clone – shallow cloning • ArrayBag b2 = (ArrayBag)bag.clone(); int manyItems 4 Object[] data bag b2 “blue” “red” “blue” “green” int manyItems 4 Object[] data COSC 2006 Bags of Objects
clone – deep cloning(not implemented in ArrayBag.java) • ArrayBag b2 = (ArrayBag)bag.clone(); int manyItems 4 Object[] data bag “blue” “red” “blue” “green” b2 “blue” “red” “blue” “green” int manyItems 4 Object[] data COSC 2006 Bags of Objects
Example: LinkedBag.java • bag of Objects implemented with linked list containing 4 String objects int manyNodes 4 head “blue” “red” “blue” “green” COSC 2006 Bags of Objects
LinkedBag constructor • LinkedBag bag = new LinkedBag(); • for Node class, see appendix E, page 766 • Node.java bag int manyNodes 0 Node head COSC 2006 Bags of Objects
add method • bag.add(“orange”); bag 5 manyNodes 4 head “blue” “blue” “red” “orange” “green” COSC 2006 Bags of Objects
countOccurrences • int c = bag.countOccurrences(“blue”); target bag “blue” manyNodes 4 head “blue” “blue” “red” “green” COSC 2006 Bags of Objects
countOccurrences – null target • int c = bag.countOccurrences(null); target bag manyNodes 4 head “blue” “blue” “green” COSC 2006 Bags of Objects
remove method • boolean r = bag.remove(“green”); target bag 3 “green” manyNodes 4 head “blue” “red” “blue” “green” COSC 2006 Bags of Objects
clone – shallow cloning • LinkedBag b3 = (LinkedBag) bag.clone() manyNodes 4 head bag “blue” “red” “blue” “green” b3 manyNodes 4 head COSC 2006 Bags of Objects
clone – deep cloning • LinkedBag b3 = (LinkedBag) bag.clone() manyNodes 4 head bag “blue” “red” “blue” “green” “blue” “red” “blue” “green” b3 manyNodes 4 head COSC 2006 Bags of Objects