If I initialise an array of person objects with this data
myPeople[0] = new Person("Alice", "Foo", 22 );
myPeople[1] = new Person("Alice", "Foo", 22 );
myPeople[2] = new Person("Bob", "Bar", 2);
myPeople[3] = new Person("Joe", "Blogs", 64);
myPeople[4] = new Person("Jane", "Joe", 42);
I want to my method to return the number of duplicates. In this case it would be 2, as Person 0 and 1 are dublicates of each other. If I were to change object 2 to be the same it should return 3. At the moment my method returns 1 wth two duplicates and 4 with three.
The method in question:
public static int searchForClones(Person[] array){
int numberOfClones=0;
for(int j =0; j<array.length-1; j++)
{
String tmp1 = array[j].getFirstName(); //Store first element of the array in tmp so it can be compared
String tmp3 = array[j].getLastName();
for(int i = 0; i<array.length-1; i++) //Loop to compare for every element in the array
{
String tmp2 = array[i].getFirstName(); //Do the same for the next element
String tmp4 = array[i].getLastName();
if(i!=j) //If i an j aren't the same element
{
if(tmp1.equals(tmp2) && tmp3.equals(tmp4) //and if they match
&& array[i].getAge()==array[i+1].getAge())
{
numberOfClones++; //increment the number of clones
}
}
}
}
return numberOfClones;
}
I'd really appreciate any help, as I think the only problem is the way I increment the number of clones. Maybe I need to check something and increment by an appropriate number after that?