aspose file tools*
The moose likes Beginning Java and the fly likes testing array equality Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "testing array equality" Watch "testing array equality" New topic
Author

testing array equality

Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
OK, I'm about to go crazy! I wrote a class that compares ClockTime - I set the class up using arrays. I want to compare two ClockTime objects and test for equality. My instance variable looks like:

private int[] myTime = new int[3];

My default constructor looks like:

public ClockTime()
myTime[0] = 0;
myTime[1] = 0;
myTime[2] = 0;

And my second constructor looks like:

public ClockTime(int hours, int minutes, int seconds)
myTime[0] = hours;
myTime[1] = minutes;
myTime[2] = seconds;

Say a user creates 2 ClockTime objects using either constructor, it doesn't matter which one. After the user creates these two objects, my equals() method needs to compare the two objects for equality - in other words, are they the same time or not.

I don't know if I'm having a brain fart or what, but I CAN'T FIGURE OUT HOW I'M SUPPOSED TO DO THIS. My assignment is due at 8:00 p.m. CST - will someone please help?!?!?!

THANKS!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
How about a for loop that checks if each coresponding element is equal?

Layne


Java API Documentation
The Java Tutorial
Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
That sounds great, but two issues (for me at least):

1. How do I specify what I'm checking? When the user creates these two objects, they can name them anything they want. They can name the objects c1 and c2, or clock1 and clock2...etc. So how does my method know what objects to check?

2. My TA mentioned using the equals() method; i.e. c1.equals(c2) I've tried this method and it's not working.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You have to override equals(Object x) with your own version. Your two arrays are referenced through this and x. You need to check that the types are correct with instanceof and check that this.length == x.length.

Finally, loop over the array elements, checking equality.
[ November 06, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Amy Lee:
That sounds great, but two issues (for me at least):

1. How do I specify what I'm checking? When the user creates these two objects, they can name them anything they want. They can name the objects c1 and c2, or clock1 and clock2...etc. So how does my method know what objects to check?

2. My TA mentioned using the equals() method; i.e. c1.equals(c2) I've tried this method and it's not working.


As Mike says above, you need to create your own equals() method. When you call c1.equals(c2), the objects being compared are given "aliases" so to speak. Inside the equals() method, c1 can be referred to with the keyword "this" (without the quotes) and c2 can be referred to with the name of the parameter (i.e. x in Mike's example above). If this isn't clear, you probably need to review how methods work in general. This "aliasing" works no matter which method you call. It isn't specific to the equals() method described here.

HTH

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: testing array equality