Peopleのリスト内でcommonDatesを見つけるプログラムを作成する必要があります。Out of BoundsException
public DateSet commonDates() {
if (persons.size() >= 2) {
DateSet Cdates =
persons.get(0).getDateSet().intersection(persons.get(1).getDateSet());
for (int i = 2; i < persons.size(); i++) {
Cdates = Cdates.intersection(persons.get(i).getDateSet());
}
return Cdates;
}
else {
throw new IllegalArgumentException();
}
}
これは交差法である:
public DateSet intersection(DateSet other) {
DateSet dates2 = (DateSet) other;
DateSet NewDateSet = new DateSet();
for(int i = 0; i < dates.size(); i++) {
if (dates.get(i).equals(dates2.dates.get(i))) {
NewDateSet.add(dates.get(i));
}
}
return NewDateSet;
}
commonDatesのためのJUnitテストは、これらは私が
感謝を取得したエラーメッセージです。この
@Test
public void testCommonDates() {
DatePicker persons = new DatePicker();
List<Date> dates = new ArrayList<Date>();
Person P1 = new Person("Joop");
Person P2 = new Person("Joopie");
Person P3 = new Person("Jaapie");
Date D1 = new Date("maandag");
Date D2 = new Date("dinsdag");
dates.add(D1);
persons.addPerson(P1);
persons.addPerson(P2);
P1.add(D1);
P2.add(D1);
P3.add(D1);
P1.add(D2);
assertThat("commonDates should return dates all persons have in common", persons.commonDates(), equalTo(dates));
}
のように見えます事前に
これを[mcve]に減らしてJavaの命名規則に従えば本当に役に立ちます。これを再現するために必要なものはすべて揃っていますが、必要以上に多くのものがあり、再現するのに便利な形ではありません。さらに、あなたの診断調査が示していることを教えてくれませんでした。 (ヒント:あなたの 'intersection'メソッドは' DateSet'の両方が同じ数のエントリーを持っていると仮定しているということが問題であると確信しています...) –
コードで言及しているほとんどのデータ型はJava Standard 、 それらは何ですか? –
forループごとにsize() - 1を試すことはできますか? –