将对象添加到数组的方法

时间:2015-03-23 20:19:43

标签: java arrays methods

您好我有一个作业,我应该创建一个Person类并为其编写必要的代码。如果我可以使用某种List,这可能会非常容易 但是,我们不允许使用List。这就是我只使用数组的原因。所以我可以编写一些代码但我仍然​​坚持使用allCousins()allAncestors()方法。我尝试了我能在互联网上找到的所有内容,这里的一些答案对我帮助很大,但我仍然得到了ArrayIndexOutOfBoundsException。所以,如果你可以帮助我,我会非常感激。

在我的作业中,我们必须编写一些方法,例如allBrothers()allSiblings()我写了这些方法并且它们有效。以下是allSiblings()的示例代码:

public Person[] allSiblings(){
        Person[] siblings = new Person[0];
        for(int i=0;i<this.mother.children.length;i++){
            if(this.mother.children[i]==this)
                continue;
            else{
                Person[] newArray = new Person[siblings.length+1];
                System.arraycopy(siblings, 0, newArray, 0, siblings.length);
                newArray[newArray.length-1] = this.mother.children[i];
                siblings = newArray;
            }
        }
    return siblings;
}

还有这个allCousins()方法返回一个Person对象数组。这是我到目前为止所尝试但无法管理它的工作。

public Person[] allCousins(){
        Person[] cousins = new Person[0];
        for(int i = 0;i<this.getParents().length;i++){
            for(int a = 0;a<this.getParents()[i].allSiblings().length;i++){
                cousins = combine(cousins, this.getParents()[i].allSiblings()[a].getChildren())
            }   
        }
        return cousins;
    }

public static Person[] combine(Person[] a, Person[] b){
        int length = a.length + b.length;
        Person[] result = new Person[length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }

在这里,我尝试将每个叔叔和阿姨的孩子阵列添加到堂兄阵列并返回但不起作用。然后我试着孩子一个孩子。但那也不起作用。这是代码,我知道这是一种愚蠢的方式,而且不必要的复杂

public Person[] allCousins(){
        Person[] cousins = new Person[0];
        for(int i = 0;i<this.father.allSiblings().length;i++){
            for(int a = 0;i<this.father.allSiblings()[i].allChildren().length;a++){
                Person[] newArray = new Person[cousins.length+1];
                System.arraycopy(cousins, 0, newArray, 0, cousins.length);
                newArray[newArray.length-1] = this.father.allSiblings()[i].allChildren()[a];
                cousins = newArray;
            }
        }
        for(int i = 0;i<this.mother.allSiblings().length;i++){
            for(int a = 0;i<this.mother.allSiblings()[i].allChildren().length;a++){
                Person[] newArray = new Person[cousins.length+1];
                System.arraycopy(cousins, 0, newArray, 0, cousins.length);
                newArray[newArray.length-1] = this.mother.allSiblings()[i].allChildren()[a];
                cousins = newArray;
            }
        }
        return cousins;
    }

我无法找到问题所在,因为allSiblings()allChildren()有效,这些是我在上面的示例中使用的唯一方法。可能我错过了for循环的东西,因为它给了ArrayIndexOutOfBoundsException但我找不到它。所以,如果你能帮助我,我会很高兴。

提前致谢

3 个答案:

答案 0 :(得分:3)

for循环中的索引错误。对于第一个allCousins()

for(int a = 0;a<this.getParents()[i].allSiblings().length;i++){
                                                          ^
                                                          should be a++

对于第二个allCousins()(对于母亲和父亲的循环):

for(int a = 0;i<this.father.allSiblings()[i].allChildren().length;a++){
              ^
              should be a

答案 1 :(得分:0)

当我进行故障排除时,我创建变量来保存作为控制语句一部分的变量。

for(int i = 0;i<this.father.allSiblings().length;i++){ ... }

我建议:

Person[] dadsSiblings = this.father.allSiblings();
int numberOfDadsSibs = dadsSiblings.length;
for(int i = 0;i<numberOfDadsSibs ;i++){...}

等等。然后,当你有一切正常工作时,你可以在必要时摆脱这些变量。如果使用调试器,则可以设置断点以查看这些变量,以确保它们具有您期望的值。如果不使用调试器,请将值打印到控制台以确保它们具有正确的值。

答案 2 :(得分:0)

我相信而不是

       for(int a = 0; i < this.father.allSiblings()[i].allChildren().length; a++)
你应该

       for(int a = 0; a < this.father.allSiblings()[i].allChildren().length; a++)

否则你a会无法控制地增长,并且在某些时候超过数组的长度。