CS 2430 Day 12
Agenda for today Container class example: DateList Growable container classes
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)
Class DateList
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 }
Implementation of class DateList
Constructors
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; }... }
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()
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()
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()
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()
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()
public class DateList {... private int find(Date inDate) {... } public boolean contains(Date inDate) { return find(inDate) >= 0; }... } Implementing contains()
public class DateList {... public Date itemAt(int index) // only if DateList is ordered { if (index = size) return null; return dates[index]; }... } Implementing itemAt()
Any questions?
What happens when we run out of room in the DateList ?
It should be a “growable” DateList !
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) {... }... }
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() {... }... }
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; }
Cannot grow public boolean add(Date inDate) { if (size >= dates.length) return false; dates[size++] = inDate; return true; } Before
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!
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
When does add() return false ?
Never!
Any questions?