• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compare all objects in an ArrayList to all others but not self  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a ArrayList of objects of class called HockeyPlayer (ArrayList<HockeyPlayer>). A HockeyPlayer has a String name and int number of goals.

This is my current work for comparing each object in the list to every other and printing them to screen:


This of course produces some duplication in the print-out:
These are the equal hockey players:
Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.

Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.

These are the equal hockey players:
Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.

Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.


How do I remove the duplication in the print-out?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing the if comparision statement to == operator will return true if compared with same object, false if compared with other objects of the similar type.
I hope this solves the problem.
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post. Always use the code tags: doesn't it look better
I also broke the long lines in both posts, so you can see how to do it correctly. You would have formatted it better than I did if you had broken the lines yourselves.
Only include code which will be executed; don't include commented‑out code.
I also removed the commented‑out code in the second post which reveals a serious logic error. Prasha Bhat had commented out a half‑line after line 6 which ended with a { That is an unexpected drawback of K&R indentation which would not have occurred under Allman indentation. In Allman indentation the { would have been on a line by itself and then lines 8-16 would have been enclosed in the inner loop. As it is only line 7 is in the inner loop.

Actually chances are you would have had unbalanced braces and there would have been a class interface or enum expected compiler error at the end of the file.

I am going to disagree, I am afraid. Don't use the == operator because you are assuming that the player objects are disjoint. i.e. there are not pairs of instances referring to the same player. Implement a proper equals method and use that.
Don't use !(something==somethingElse). Use something != somethingElse.
Don't try to extract details from the player objects in the loop. Give the player class a proper toString method and call that. Like this:-
System.out.println(player);
Don't use empty Strings. Not System.out.println(""); but System.out.println();
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And line 7 would print equal after you have tested whether they are not equal.
I was mistaken about the line 7 being the only line in the loop; it is of course the only line inside the if.
Sorry about that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!