简体   繁体   English

访问类数组字段

[英]Accessing class array field

I'm having problems accessing a field from an array in a for-loop. 我在for循环中无法从数组访问字段。 I'm sure I'm just messing up the syntax: 我确定我只是弄乱了语法:

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
}

EDIT: My code contained logic errors (compared the wrong persons). 编辑:我的代码包含逻辑错误(与错误的人相比)。 Will this safety check work, or will it yield an error if pers1 does not exist when it checks if pers1's mother has a name? 此安全检查有效吗?如果检查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

You swapped two things, pers1.children.name.get(i) should be pers1.children.get(i).name . 您交换了两件事, pers1.children.name.get(i)应该是pers1.children.get(i).name

Since we're here, you are comparing strings with == , this compare references of objects, use equals(..) instead. 既然我们在这里,您将比较==字符串,此比较对象的引用,请使用equals(..) Check it out here . 在这里查看

Looks like you're using == instead of equals() function to compare strings. 看起来您正在使用==而不是equals()函数来比较字符串。

Try changing this line like the following: 尝试如下更改此行:

if (pers1.children.name.get(i).equals(name))

Don't use "==" for String comparison; 不要使用“ ==”进行字符串比较; it's not doing what you think. 它没有按照您的想法做。 You want to write it as: 您希望将其编写为:

if (mother.name.equals(pers1.name)) { ...

Of course, that's a slightly dangerous approach, as you'll get NullPointerException s if either mother or mother.name are null. 当然,这是一种稍微危险的方法,因为如果mothermother.name为null,则会得到NullPointerException So you'll want to do a little defensive programming. 因此,您需要进行一些防御性编程。

if (mother == null) return false;
if (pers1 == null) return false;
if (mother.name != null && mother.name.equals(pers1.name)) ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM