user121111
user121111

Reputation: 19

A method to add an object to an array

Hello I have an assignment which I should create a Person class and write necessary codes for it. This could be so easy if i could use some kind of List however, we are not allowed to use List . That's why I am using only arrays. So i could write some codes but I'm stuck on allCousins() and allAncestors() methods. I tried everything i could find on internet and some answers here helped me a lot but still, I'm getting ArrayIndexOutOfBoundsException. So if you could help me on that, I'd be so appreciated.

In my assignment we have to write some methods like allBrothers(), allSiblings()I wrote these ones and they work. Here is an example code of 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;
}

And there is this allCousins() method which returns an array of Person objects. here is what i tried so far but couldn't manage it to work.

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;
    }

Here i tried to add every uncle's and aunt's children array to cousins array and return it but didn't work. Then i tried to do it child by child. But that didn't work also. Here is the code, I know it's a stupid way to do it and unnecessarily complicated

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;
    }

I couldn't find where the problem is because allSiblings() and allChildren() works and these are the only methods i used in examples above. Probably i am missing something in for loops because it gives an ArrayIndexOutOfBoundsException but i can't find it. So if you can help me, I'd be so glad.

Thanks in advance

Upvotes: 0

Views: 67

Answers (3)

PM 77-1
PM 77-1

Reputation: 13334

I believe that instead of

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

you should have

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

otherwise you a can grow uncontrollably and at some point exceed the length of the array.

Upvotes: 0

T.Gounelle
T.Gounelle

Reputation: 6033

The indexes are wrong in your for loops. For the first allCousins():

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

And for the second allCousins() (for mother and father loops):

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

Upvotes: 3

wdb
wdb

Reputation: 343

When I am troubleshooting, I create variables to hold variables that are part of control statements. You have

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

I suggest this:

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

and so on. Then, when you have got everything working, you can get rid of such variables if necessary. If you use a debugger, you can set breakpoints to look at those those variables to make sure that they have the values you expect. If you don't use a debugger, print the values to the console to make sure that they have the right values.

Upvotes: 0

Related Questions