cs 2430 day 12. agenda for today container class example: datelist growable container classes
TRANSCRIPT
CS 2430
Day 12
Agenda for today
• Container class example: DateList• Growable container classes
List of Dates ADT
Data:• Default maximum size
(constant)• Array of Dates• Current number of Dates 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) { . . . }
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 contains(Date inDate) { return find(inDate) >= 0; } . . .}
Implementing contains()
public class DateList{ . . .
public Date itemAt(int index) // only if DateList is ordered { if (index < 0 || 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 growpublic 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/93 7/8/99 2/4/96
dates
►
►
►
►
►
i
1/2/931/2/93
3/4/95
3/4/953/4/95 7/8/997/8/99
1/3/95
1/3/951/3/95
2/4/96
2/4/962/4/96
When does add() return false?
Never!
Any questions?