How can I compare class objects for equality?

  • Thread starter Thread starter russel.arnold
  • Start date Start date
  • Tags Tags
    Class
AI Thread Summary
To compare multiple instances of a class for equality based on their attributes, it's essential to override the equals method in the class. This method should compare the string attributes (s and g) and the integer attribute (i) of the objects. Using a loop is not suitable for direct comparison of two objects; instead, the equals method should handle the logic for checking if the attributes of two instances are identical. This approach provides a clear and efficient way to determine equality without unnecessary complexity.
russel.arnold
Messages
38
Reaction score
0
Hi,

I have a class, say myclass and it takes two strings (let s and g ) and one int ( say i) as its arguments.

It looks like, public class {

String s , g;
int i ;

public void myclass(String sname, String gname, int k){

s = sname;
g = gname;
i = k;
}
}

Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?
 
Technology news on Phys.org
russel.arnold said:
Hi,

I have a class, say myclass and it takes two strings (let s and g ) and one int ( say i) as its arguments.

It looks like, public class {

String s , g;
int i ;

public void myclass(String sname, String gname, int k){

s = sname;
g = gname;
i = k;
}
}

Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?

I'm pretty sure object equality has to be done manually by overriding the equality operator and comparing each field of the two objects.
 
russel.arnold said:
Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?
A loop isn't appropriate at all. There is no way you can write a loop that will compare two strings and an int in one class instance with variables of the same types in another class instance. You will need to do a comparison in the way that DavidSnider suggests.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top