1 / 25

Genericity

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

nerita
Télécharger la présentation

Genericity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Genericity collections of objects

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

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

  4. bags of Objects • implemented with array • ArrayBag.java • implemented with linked list • LinkedBag.java COSC 2006 Bags of Objects

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

  6. ArrayBag constructor • ArrayBag a = new ArrayBag(5); a int manyItems 0 Object[] data COSC 2006 Bags of Objects

  7. ArrayBag constructor • ArrayBag bag = new ArrayBag(); bag int manyItems 0 Object[] data COSC 2006 Bags of Objects

  8. add method • bag.add(“orange”); bag 5 int manyItems 4 Object[] data “green” “blue” “red” “blue” “orange” COSC 2006 Bags of Objects

  9. add method – null object • bag.add(null); bag 5 int manyItems 4 Object[] data “green” “blue” “red” “blue” COSC 2006 Bags of Objects

  10. countOccurrences • int c = bag.countOccurrences(“blue”); target bag “blue” int manyItems 4 Object[] data “green” “blue” “red” “blue” COSC 2006 Bags of Objects

  11. countOccurrences – null target • int c = bag.countOccurrences(null) target bag int manyItems 4 Object[] data “green” “blue” “blue” COSC 2006 Bags of Objects

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

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

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

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

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

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

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

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

  20. add method • bag.add(“orange”); bag 5 manyNodes 4 head “blue” “blue” “red” “orange” “green” COSC 2006 Bags of Objects

  21. countOccurrences • int c = bag.countOccurrences(“blue”); target bag “blue” manyNodes 4 head “blue” “blue” “red” “green” COSC 2006 Bags of Objects

  22. countOccurrences – null target • int c = bag.countOccurrences(null); target bag manyNodes 4 head “blue” “blue” “green” COSC 2006 Bags of Objects

  23. remove method • boolean r = bag.remove(“green”); target bag 3 “green” manyNodes 4 head “blue” “red” “blue” “green” COSC 2006 Bags of Objects

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

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

More Related