for ループで配列からフィールドにアクセスする際に問題があります。私は構文を台無しにしているだけだと確信しています:
public class Person {
String name;
Person mother;
Person father;
ArrayList<Person> children;
public boolean isMotherOf(Person pers1) {
//First check if Persons mother equals pers1
if (mother.name == pers1.name) {
//Second checks if pers1s children contains Person
for (int i = 0; i < pers1.children.size(); i++) {
if (pers1.children.name.get(i) == name) {
// ERROR ON THE LINE ABOVE: "name cannot be resolved or it not a field"
return true;
}
} // END second check
} else { return false; }
} // END method isMotherOf
}
編集:私のコードには論理エラーが含まれていました(間違った人を比較して)。この安全性チェックは機能しますか? または、pers1 の母親に名前があるかどうかをチェックするときに、pers1 が存在しない場合にエラーが発生しますか?
public class Person {
String name;
Person mother;
Person father;
ArrayList<Person> children;
// Checks if THIS person is the mother of pers1
public boolean isMotherOf(Person pers1) {
// Safety check: Both persons exists and Person has children and pers1 has mother
if (name == null || children == null || pers1 == null
|| pers1.mother.name == null) {
return false;
} // END safety check
// First check if Persons name equals pers1's mother
else if (name.equals(pers1.mother.name)) {
// Second check if Persons children contains pers1
for (int i = 0; i < children.size(); i++) {
if (children.get(i).name.equals(pers1.name)) {
return true;
}
} // END second check
} // END first check
return false;
} // END method isMotherOf
} // END class Person