Title: More Containers
1More Containers
2Method Locate
/ ... _at_param obj target to be found _at_return
Object reference if found and null
otherwise / public Object locate( Object obj
) int pos find( obj ) if ( pos gt 0
) return listpos return null
3Method Locate
Bag b new Bag( 10 ) ... // Assume we have
Person class Person p1 new Person() // Does
it work? Person p2 b.locate ( p1 ) NO! Method
locate() returns an Object reference! public
Object locate( Object obj ) We can assign a
child to parent. We CANT assign a parent to a
child. Need a CAST.
4Method Locate
Bag b new Bag( 10 ) ... Person p1 new
Person() Object obj b.locate ( p1 ) if ( obj
! null obj instanceof Person ) Person p2
(Person)obj System.out.println(
p2.getAddress() ) ... Cast obj to Person
when it points to a Person. public Object locate(
Object obj )
5C Template
In C , youll have templates. You can write a
general container, but instantiate it to only
take specific types. Java now has generics, and
we will use generic Java containers later on in
the semester.
6SortedList Class
The data declaration can be the same as that for
the Bag. private Object list private
int num 0 private final int GROWBY 3
All the Bag methods we wrote would also work as
SortedList methods, except the add and the
delete. Both add and delete must keep list in
sorted order.
7Method add
index
0 1 2
num-1 length-1
New item
Find the position to be added Move items
down one position Add the new item
Update num
8Method delete
index
0 1 2
num-1 length-1
Find the item (index) to be deleted Move
items up one position Update num
9Methods add and delete
Both add and delete must keep list in sorted
order. Could use the simple implementation for
Bag Call a private sort method after adding and
removing Can use either Linear Search or Binary
Search
10SortedList Class
New method public Object itemAt ( int index
) if ( index lt 0 index gt num )
return null return listindex Not
applicable to Bag class
11Usage Example
SortedList l new SortedList( 10 ) // add and
remove items // get index Object obj l.itemAt
( index ) if ( obj null ) ... else if (
obj instanceof Person ) ... else if ( onj
instanceof Date ) ...
12Set Class
- An item can appear in a set at most once.
- The data declaration can be the same as that for
the Bag. - private Object list
- private int num 0
- private final int GROWBY 3
- All methods can be the same as Bag except
- add - dont add if already in set
- count (number of times an item in the container)
- not needed (just use contains)
- deleteAll not needed (just use delete)
13New Methods for Set Class
- intersection
- union
- setDifference
- isSubset
14Method add
public boolean add( Object obj ) if (
contains( obj ) ) // in the set already
return false if ( num gt list.length ) //
Its full grow() listnum obj //
store the reference return true
15Method interesection
public Set intersection( Set s ) Set temp
new Set( num ) for (int i 0 i lt num i
) if ( s.contains( listi ) )
temp.add( listi ) return temp Other
methods are similar. Returns a set. Does not
change the current object.
16Usage Example
Set s1, s2, s3 s1 new Set( 5 ) s2 new Set
( 10 ) // add and remove items if
(s1.isSubset( S2 ) ) ... s3
s1.intersection( s2 )
17Next Week
- Monday
- Quiz2
- Tuesday
- Lab/Prog3
- Wednesday
- Prog3
- Thursday
- Prog3 Work Plan due
- Friday
- Test 1 (70 points)
18Program 3