More Containers - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

More Containers

Description:

Method locate() returns an Object reference! public Object locate( Object obj ) ... Call a private sort method after adding and removing ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 19
Provided by: qya
Category:

less

Transcript and Presenter's Notes

Title: More Containers


1
More Containers
2
Method 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
3
Method 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.
4
Method 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 )
5
C 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.
6
SortedList 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.
7
Method 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
8
Method delete
index
0 1 2
num-1 length-1
Find the item (index) to be deleted Move
items up one position Update num
9
Methods 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
10
SortedList Class
New method public Object itemAt ( int index
) if ( index lt 0 index gt num )
return null return listindex Not
applicable to Bag class
11
Usage 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 ) ...
12
Set 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)

13
New Methods for Set Class
  • intersection
  • union
  • setDifference
  • isSubset

14
Method 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
15
Method 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.
16
Usage 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 )
17
Next Week
  • Monday
  • Quiz2
  • Tuesday
  • Lab/Prog3
  • Wednesday
  • Prog3
  • Thursday
  • Prog3 Work Plan due
  • Friday
  • Test 1 (70 points)

18
Program 3
Write a Comment
User Comments (0)
About PowerShow.com