25
Genericity collections of objects

Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects for real programming applications, pratical collections are collections

Embed Size (px)

Citation preview

Page 1: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

Genericity

collections of objects

Page 2: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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

Page 3: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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

Page 4: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 4

bags of Objects

implemented with array ArrayBag.java

implemented with linked list LinkedBag.java

Page 5: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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”

Page 6: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 6

ArrayBag constructor

ArrayBag a = new ArrayBag(5);

int manyItems

Object[] data

0

a

Page 7: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 7

ArrayBag constructor

ArrayBag bag = new ArrayBag();

int manyItems

Object[] data

0

bag

Page 8: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 8

add method

bag.add(“orange”);

int manyItems

Object[] data

4

“blue” “red” “blue” “green” “orange”

5

bag

Page 9: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 9

add method – null object

bag.add(null);

int manyItems

Object[] data

4

“blue” “red” “blue” “green”

5

bag

Page 10: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 10

countOccurrences

int c = bag.countOccurrences(“blue”);

int manyItems

Object[] data

4

“blue” “red” “blue” “green”

target

“blue”

bag

Page 11: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 11

countOccurrences – null target

int c = bag.countOccurrences(null)

int manyItems

Object[] data

4

“blue” “blue” “green”

targetbag

Page 12: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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

Page 13: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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

Page 14: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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

Page 15: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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

Page 16: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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

Page 17: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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”

Page 18: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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”

Page 19: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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

Page 20: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 20

add method

bag.add(“orange”);

manyNodes

head

4

“blue”

“red” “blue”“green”“orange”

5bag

Page 21: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 21

countOccurrences

int c = bag.countOccurrences(“blue”);

manyNodes

head

4

“blue”“red” “blue”

“green”

target

“blue”bag

Page 22: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 22

countOccurrences – null target

int c = bag.countOccurrences(null);

manyNodes

head

4

“blue”“blue”

“green”

target

bag

Page 23: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

COSC 2006 Bags of Objects 23

remove method

boolean r = bag.remove(“green”);

manyNodes

head

4

“blue”“red” “blue”

“green”

target

“green”3

bag

Page 24: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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

Page 25: Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections

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”