Genericity
collections of objects
COSC 2006 Bags 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
Object[] data
4
“blue” “red” “blue” “green”
COSC 2006 Bags of Objects 6
ArrayBag constructor
ArrayBag a = new ArrayBag(5);
int manyItems
Object[] data
0
a
COSC 2006 Bags of Objects 7
ArrayBag constructor
ArrayBag bag = new ArrayBag();
int manyItems
Object[] data
0
bag
COSC 2006 Bags of Objects 8
add method
bag.add(“orange”);
int manyItems
Object[] data
4
“blue” “red” “blue” “green” “orange”
5
bag
COSC 2006 Bags of Objects 9
add method – null object
bag.add(null);
int manyItems
Object[] data
4
“blue” “red” “blue” “green”
5
bag
COSC 2006 Bags of Objects 10
countOccurrences
int c = bag.countOccurrences(“blue”);
int manyItems
Object[] data
4
“blue” “red” “blue” “green”
target
“blue”
bag
COSC 2006 Bags of Objects 11
countOccurrences – null target
int c = bag.countOccurrences(null)
int manyItems
Object[] data
4
“blue” “blue” “green”
targetbag
COSC 2006 Bags of Objects 12
remove method
boolean r = bag.remove(“blue”);
int manyItems
Object[] data
4
“blue” “red” “blue” “green”
target
“blue”3
bag
COSC 2006 Bags of Objects 13
remove method – null target
boolean r = bag.remove(null);
int manyItems
Object[] data
“blue” “blue” “green”
target
4
3
bag
COSC 2006 Bags of Objects 14
returning an object
Object o = bag.grab(); String s = (String) o;
int manyItems
Object[] data
4
“blue” “red” “blue” “green”
Object o
String s
bag
COSC 2006 Bags of Objects 15
returning an object
String s = (String) bag.grab();
int manyItems
Object[] data
4
“blue” “red” “blue” “green”
String s
bag
COSC 2006 Bags of Objects 16
clone – shallow cloning
ArrayBag b2 = (ArrayBag)bag.clone();
int manyItems
Object[] data
4
“blue” “red” “blue” “green”
int manyItems
Object[] data
4
bag
b2
COSC 2006 Bags of Objects 17
clone – deep cloning(not implemented in ArrayBag.java)
ArrayBag b2 = (ArrayBag)bag.clone();int manyItems
Object[] data
4
“blue” “red” “blue” “green”
int manyItems
Object[] data
4
bag
b2 “blue” “red” “blue” “green”
COSC 2006 Bags of Objects 18
Example: LinkedBag.java
bag of Objects implemented with linked list containing 4 String objects
int manyNodes
head
4
“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
int manyNodes
Node head
0
bag
COSC 2006 Bags of Objects 20
add method
bag.add(“orange”);
manyNodes
head
4
“blue”
“red” “blue”“green”“orange”
5bag
COSC 2006 Bags of Objects 21
countOccurrences
int c = bag.countOccurrences(“blue”);
manyNodes
head
4
“blue”“red” “blue”
“green”
target
“blue”bag
COSC 2006 Bags of Objects 22
countOccurrences – null target
int c = bag.countOccurrences(null);
manyNodes
head
4
“blue”“blue”
“green”
target
bag
COSC 2006 Bags of Objects 23
remove method
boolean r = bag.remove(“green”);
manyNodes
head
4
“blue”“red” “blue”
“green”
target
“green”3
bag
COSC 2006 Bags of Objects 24
clone – shallow cloning
LinkedBag b3 = (LinkedBag) bag.clone()
manyNodes
head
4
“blue” “red” “blue” “green”
manyNodes
head
4
bag
b3
COSC 2006 Bags of Objects 25
clone – deep cloning
LinkedBag b3 = (LinkedBag) bag.clone()manyNodes
head
4
“blue” “red” “blue” “green”
manyNodes
head
4
bag
b3“blue” “red” “blue” “green”