Download presentation
Presentation is loading. Please wait.
Published byAnthony Parsons Modified over 6 years ago
1
COMPUTER 2430 Object Oriented Programming and Data Structures I
2
Container Classes Bag Set Sorted List . . .
3
Class Bag Container Can grow, but never shrink
The order of array elements is not maintained May or may not allow duplicate elements
4
public class Bag { private final int INIT_SIZE = ; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; public void addObject( Object obj ) public boolean removeObject( Object obj ) private int find ( Object obj ) @Override public String toString () private void grow () }
5
public class CS2430Sx { public static void main(String[] args) Bag myBag = new Bag(); Object obj = new Object(); // Valid? myBag.addObject(obj); myBag.addObject(new Date()); myBag.addObject(new Student(“Frank”)); }
6
public class Bag { private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; public void addObject( Object obj ) if (numObjects == bag.length) grow(); bag[numObjects ++] = obj; } private void grow () int size = bag.length + GROWBY; Object[] temp = new Object[size]; for (int i = 0; i < numObjects; i ++) temp[i] = bag[i]; bag = temp;
7
public class Bag { . . . // Allow duplicate? // Yes public void addObject( Object obj ) if (numObjects == bag.length) grow(); bag[numObjects ++] = obj; }
8
public class Bag { . . . // What if duplicates are not allowed? public boolean addObject( Object obj ) if (find(obj) != -1) return false; if (numObjects == bag.length) grow(); bag[numObjects ++] = obj; return true; } private void grow () private int find( Object obj )
9
It works for all classes: Polymorphism!
public class Bag { private Object[] bag = new Object[INIT_SIZE]; /** Finds and returns the index of element in array bag that equals obj. @param obj the object to be found @return the index of the element if found, -1 otherwise */ private int find( Object obj ) for (int i = 0; i < numObjects; i ++) if (bag[i].equals(obj)) return i; return -1; } It works for all classes: Polymorphism!
10
public class Bag { private final int INIT_SIZE = ; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; // Maintain order public boolean removeObject( Object obj ) int index = find (obj); if (index > -1) -- numObjects; for (int i = index; i < numObjects; i ++) bag[i] = bag[i + 1]; return true; } return false;
11
// Maintain order public boolean removeObject( Object obj ) { int index = find (obj); if (index > -1) delete(index); return true; } return false; // Quiz 2 // Index is in the range and the operation will be successful! public void delete ( int index ) -- numObjects; for (int i = index; i < numObjects; i ++) bag[i] = bag[i + 1];
12
public class Bag { private final int INIT_SIZE = ; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; // Do not maintain order public boolean removeObject( Object obj ) int index = find (obj); if (index > -1) bag[index] = bag[-- numObjects]; return true; } return false;
13
// Remove one object from the array
public boolean removeObject( Object obj ) { int index = find (obj); if (index > -1) delete(index); return true; } return false;
14
// Remove all objects from the array that equal obj
public int removeObject( Object obj ) { int count = 0; int index = find (obj); while (index > -1) delete(index); count ++; index = find(obj); } return count;
15
Any Other Meaningful Bag Methods?
public class Bag { private final int INIT_SIZE = 10000; private final int GROWBY = 100; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; public void addObject( Object obj ) private void grow () public boolean removeObject( Object obj ) public int find ( Object obj ) @Override public String toString () } Any Other Meaningful Bag Methods?
16
public class Bag { private final int INIT_SIZE = 10000; private final int GROWBY = 100; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; . . . public boolean isEmpty() return numObjects == 0; } public int getCount () return numObjects;
17
public class Bag { private Object[] bag = new Object[INIT_SIZE]; public Object getItem ( Object obj ) int index = find(obj); if ( index != -1 ) return bag[index]; return null; } Why we need this method?
18
GolfLeague theLeague = new GolfLeague();
GolfLeagueMember member = new SeniorMember(“Qi”, 80); System.out.println(member.toString()); // The scores and handicap for Qi has changed . . . member = theLeague.getItem(member);
19
SortedList Class The data declaration can be the same as that for the Bag. private Object[] list = new Object[INIT_SIZE]; private int num = 0; private final int GROWBY = 3; All the Bag methods we wrote would also work as SortedList methods, but the list must be in sorted order.
20
Method add Assume in ascending order. Where to insert a new object?
index … i i num length-1 Assume in ascending order. Where to insert a new object? list[0] < obj list[1] < obj . . . list[i] < obj list[i+1] > obj Need a comparison method!
21
Method add Find the position to be added Move items down one position
index num length-1 … New item … Find the position to be added Move items down one position Add the new item Update num
22
Method delete Find the item (index) to be deleted
num length-1 … … Find the item (index) to be deleted Move items up one position Update num
23
Sort Method
24
Search Method
25
Quiz 2
26
Lab 3 Submission
27
Prog 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.