I'm writing Java code for an old style phone plan, so I have:
- a Band class:
public Band(LocalTime startTime, LocalTime endTime, DayOfWeek[] combinedDays, double intervalCost)
- a Rate class:
public Rate(String name, Band[] bands, int intervalMs, double startCost, String numberRoot)
I want to write a private Band[] selectBandsInDay(DayOfWeek day)
method inside the Rate class that, given a day of week, returns an array of Band composed of the bands of that day of week.
What I wrote was:
private Band[] selectBandsInDay(DayOfWeek day) {
Band[] bandsInDay = new Band[bands.length];
int size = 0;
for (int i=0; i<bands.length; i++) {
for (int j=0; j<bands.length; j++) {
if (bands[j].getCombinedDays()[i] == day) {
bandsInDay[size] = bands[i];
size++;
}
}
}
return bandsInDay;
}
But I keep getting an Index Out Of Bounds exception (index 2 out of bounds for length 2).
How could I fix this?