Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why does Object.equals() fail to match two exactly the same Objects

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
Hi.
My problem is, I'm trying to match up objects which are exactly the same in two different arrays.
Before you see the code, you should understand that a client instance holds a reference to a vacation instance.
I'm trying to see how many other lients have a refernce to the same vacation instance.
--------------------Method to match objects----------------
public void addClientToVacation(int index) throws IOException, ClassNotFoundException
{
System.out.println("Vacations to chose from:\n");
for (int i = 0;i < vacationArray.nextFreeLocation;i++)
{
System.out.print(&quot;-------------------------------------------\n&quot;
+ i + &quot;: &quot; + vacationArray.vacations.getHolType() + &quot; in &quot;
+ vacationArray.vacations.getArea() + &quot; during the &quot;
+ vacationArray.vacations.getTimeOfYear()
+ &quot;\n-------------------------------------------\n\n&quot;);
}
System.out.print(&quot;Vacation to assign to client \&quot;&quot;
+ tempArray.clients[index].getSurname() + &quot;\&quot;: &quot;);
int option = 0;
option = Integer.parseInt(input.readLine());
tempArray.clients[index].setDestination(vacationArray.vacations[option]);
writeIt();
System.out.print(&quot;\n\n&quot;);
clear(50);
System.out.println(&quot;Client: &quot; + &quot;\&quot;&quot; + tempArray.clients[index].getSurname() + &quot;\&quot;&quot;
+ &quot;is going &quot; + vacationArray.vacations[option].getHolType() + &quot; in &quot;
+ vacationArray.vacations[option].getArea() + &quot; during the &quot;
+ vacationArray.vacations[option].getTimeOfYear());
System.out.println(&quot;\nOther people going on this vacation are:&quot;);
String output = &quot;&quot;;
for (int i = 0;i < tempArray.nextFreeLocation;i++)
{ //destination in client constructor is made to null
System.out.println(&quot;place 1 &quot; + i);
if (tempArray.clients.getDestination() == null)
{
System.out.println(&quot;place 2 &quot; + i);
continue;
}
System.out.println(&quot;place 3 &quot; + i + &quot;destination = &quot; + tempArray.clients.getDestination());
if (tempArray.clients.getDestination().equals(vacationArray.vacations[option])) {
System.out.println(&quot;place 4 &quot; + i);
output = output + tempArray.clients.getSurname() + &quot;\n&quot;;
}
System.out.println(&quot;place 5 &quot; + i);
}
System.out.print(output);
clear(5);
mainMenu();
return;

}
----------------------------------------------------------
It's the &quot; if (tempArray.clients.getDestination().equals(vacationArray.vacations[option])) &quot; bit towards the bottom that reffuses to match up objects.
I do know that Object.equals() when comparing objects, is the most strict method, but why is it not matching???
Here's the output I get:
--------------------Output------------------------------
Client: &quot;Burt&quot;is going Hiking in New Hampshire during the Autumn

Other people going on this vacation are:
place 1 0
place 3 0destination = Area: New Hampshire
Holiday Type: Hiking
Minimum Cost: £200
Maximum Cost: £300
Time Of Year: Autumn

place 5 0
place 1 1
place 2 1
place 1 2
place 2 2
place 1 3
place 3 3destination = Area: New Hampshire
Holiday Type: Hiking
Minimum Cost: £200
Maximum Cost: £300
Time Of Year: Autumn

place 4 3
place 5 3
place 1 4
place 2 4
place 1 5
place 2 5
Burt
--------------------------------------------------------
&quot;Burt&quot; is the actual output I would expect the method to produce, since this is the client I set the specific vacation to, but I would like to point out that place x is the flag so I can see how far each iteration goes, the number after that is the array index of the customers.
It should pick up that array element '3' holds exactly the same instance and place Wardell before &quot;Burt&quot;.

Anyway, If anyone understood that I'd just like to see what anyone thinks is the problem?? or if there's another way to do it...
Cheers every1.
Oxi we are all of us living in the gutter.
But some of us are looking at the stars.
 
Don't know what 'type' is returned:

>> tempArray.clients.getDestination()
>> vacationArray.vacations[option]

Since they are what you are testing using equals() it might help to know what type they are.

>> for (int i = 0;i < tempArray.nextFreeLocation;i++)

You are using 'i' as a loop iterator but it is never applied to 'tempArray' in any fashion in the loop??

>> tempArray.clients[index].getSurname()

So based on that line of code the variable 'clients' would seem to be an Array, so.... how do these work??

>> tempArray.clients.getDestination()
>> tempArray.clients.getSurname()

-pete
 
He forgot to put his code in [ignore]
Code:
...
[/ignore] tags so the [ignore][/ignore] in arrays are being used as italic. Oxymoron, always post your code between [ignore]
Code:
...
[/ignore] tags because most people won't bother to tell you've done it wrong. Repost!

MarsChelios
 
Code:
public void addClientToVacation(int index) throws IOException, ClassNotFoundException
    {
        System.out.println(&quot;Vacations to chose from:\n&quot;);
        for (int i = 0;i < vacationArray.nextFreeLocation;i++)
        {
            System.out.print(&quot;-------------------------------------------\n&quot;
							 + i + &quot;: &quot; + vacationArray.vacations[i].getHolType() + &quot; in &quot;
                             + vacationArray.vacations[i].getArea() + &quot; during the &quot;
                             + vacationArray.vacations[i].getTimeOfYear()
							 + &quot;\n-------------------------------------------\n\n&quot;);
        }
        System.out.print(&quot;Vacation to assign to client  \&quot;&quot;
                         + tempArray.clients[index].getSurname() + &quot;\&quot;:  &quot;);
        int option = 0;
		option = Integer.parseInt(input.readLine());
        tempArray.clients[index].setDestination(vacationArray.vacations[option]);
        writeIt();
        System.out.print(&quot;\n\n&quot;);
        clear(50);
        System.out.println(&quot;Client: &quot; + &quot;\&quot;&quot; + tempArray.clients[index].getSurname() + &quot;\&quot;&quot;
                           + &quot;is going &quot; + vacationArray.vacations[option].getHolType() + &quot; in &quot;
                           + vacationArray.vacations[option].getArea() + &quot; during the &quot;
                           + vacationArray.vacations[option].getTimeOfYear());
        System.out.println(&quot;\nOther people going on this vacation are:&quot;);
        String output = &quot;&quot;;
		for (int i = 0;i < tempArray.nextFreeLocation;i++)
        {   //destination in client constructor is made to null
            System.out.println(&quot;place 1 &quot; + i);
			if (tempArray.clients[i].getDestination() == null)
            {
                System.out.println(&quot;place 2 &quot; + i);
                continue;
            }
            System.out.println(&quot;place 3 &quot; + i + &quot;destination = &quot; + tempArray.clients[i].getDestination());
            if (tempArray.clients[i].getDestination().equals(tempArray.clients[index].getDestination())) //maybe: vacationArray.vacations[option]
            {
                System.out.println(&quot;place 4 &quot; + i);
            	output = output + tempArray.clients[i].getSurname() + &quot;\n&quot;;
            }
            System.out.println(&quot;place 5 &quot; + i);
        }
        System.out.print(output);
        clear(5);
        mainMenu();
        return;

    }
----------------------------------------------------------

And the 'type' I am trying to apply the .equals() to is that of an object.
I'm trying to find two instances of the same holiday, which are referenced by an array of client instances we are all of us living in the gutter.
But some of us are looking at the stars.
 
Oxymoron,
It might be informative to let you know that the default
Code:
equals (Object)
from
Code:
Object
only checks whether the objects are the same, meaning both reference the the same instance, and not the internal data. This means that if you create 2 instances of Vacation or whatever it is you are doing,
Code:
equals (Object)
will return false since they are different instances.
To change this default behavior, I would suggest overiding
Code:
equals (Object)
to give it the behavior you require.

Hope this Helps,
MarsChelios
 
>> And the 'type' I am trying to apply the .equals() to is
>> that of an object.

I was asking for the most derived type.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top