Download presentation
Presentation is loading. Please wait.
Published byDora Powers Modified over 9 years ago
1
CS 2430 Day 12
2
Agenda for today Container class example: DateList Growable container classes
3
List of Date s ADT Data: Default maximum size (constant) Array of Date s Current number of Date s in container Operations: Add a date Remove a date Size Contains a date? Item at (ordered list)
4
Class DateList
5
public class DateList { public static final int DEFAULT_NUM = 10; private Date[] dates; private int size; public DateList() {... } public DateList(int inSize) {... } public int size() {... } public boolean add(Date inDate) {... } public boolean remove(Date inDate) {... } public boolean contains(Date inDate) {... } public Date itemAt(int index) {... } // is DateList ordered?... // we can add other methods later }
6
Implementation of class DateList
7
Constructors
8
public class DateList { public static final int DEFAULT_NUM = 10; private Date[] dates; private int size; public DateList() { dates = new Date[DEFAULT_NUM]; size = 0; } public DateList(int inSize) { if (inSize < 0) dates = new Date[DEFAULT_NUM]; else dates = new Date[inSize]; size = 0; }... }
9
public class DateList {... public boolean add(Date inDate) { if (size >= dates.length) // can't use DEFAULT_NUM -- WHY? return false; dates[size++] = inDate; return true; }... } Implementing add()
10
public class DateList {... private int find(Date inDate) { for (int i = 0; i < size; i++) if (dates[i].equals(inDate)) return i; return -1; }... } Implementing find()
11
public class DateList {... private int find(Date inDate) { for (int j = 0, k = size – 1; k > 0; k = k / 2, j++) { for (int i = j; i < size; i += k) { if (dates[i].equals(inDate)) return i; } } return -1; }... } Weird find()
12
public class DateList {... private int find(Date inDate) {... } public boolean remove(Date inDate) // if DateList is ordered { int index = find(inDate); if (index < 0) return false; for (int i = index; i < size – 1; i++) dates[i] = dates[i + 1]; size--; return true; }... } Implementing remove()
13
public class DateList {... private int find(Date inDate) {... } public boolean remove(Date inDate) // if DateList if not ordered { int index = find(inDate); if (index < 0) return false; dates[index] = dates[size - 1]; size--; return true; }... } Another remove()
14
public class DateList {... private int find(Date inDate) {... } public boolean contains(Date inDate) { return find(inDate) >= 0; }... } Implementing contains()
15
public class DateList {... public Date itemAt(int index) // only if DateList is ordered { if (index = size) return null; return dates[index]; }... } Implementing itemAt()
16
Any questions?
17
What happens when we run out of room in the DateList ?
18
It should be a “growable” DateList !
19
public class DateList { public static final int DEFAULT_NUM = 10; private Date[] dates; private int size; public DateList() {... } public DateList(int inSize) {... } public int size() {... } public boolean add(Date inDate) {... } public boolean remove(Date inDate) {... } public boolean contains(Date inDate) {... } public Date itemAt(int index) {... }... }
20
public class GrowableDateList { public static final int GROWBY = 10; // change constructors! private Date[] dates; private int size; public GrowableDateList() {... } public GrowableDateList(int inSize) {... } public int size() {... } public boolean add(Date inDate) {... } public boolean remove(Date inDate) {... } public boolean contains(Date inDate) {... } public Date itemAt(int index) {... } private void grow() {... }... }
21
New method: grow() private void grow() { // how to increase size of array dates? Date temp[] = new Date[size + GROWBY]; for (int i = 0; i < size; i++) temp[i] = dates[i]; // don't forget: dates = temp; }
22
Cannot grow public boolean add(Date inDate) { if (size >= dates.length) return false; dates[size++] = inDate; return true; } Before
23
add() uses grow() public boolean add(Date inDate) { if (size >= dates.length) return false; dates[size++] = inDate; return true; } Before public boolean add(Date inDate) { if (size >= dates.length) grow(); dates[size++] = inDate; return true; } Now!
24
Watching arrays grow() private void grow() { // how to increase size of array dates? Date temp[] = new Date[size + GROWBY]; for (int i = 0; i < dates.length; i++) temp[i] = dates[i]; // don't forget: dates = temp; } 1/2/937/8/992/4/96 dates ► ► ► ► ► i 1/2/93 3/4/95 7/8/99 1/3/95 2/4/96
25
When does add() return false ?
26
Never!
27
Any questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.