• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding a specific object inside an array of objects  RSS feed

 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you have a code fragment that counts the # of occurrences of a specific “target” integer in an array.



Let's say I want the data array to be an array of objects and target is a non-null reference to an object with an equals method. Is this an effective way to rewrite the code presented above?

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two are different. In one case you are counting multiple instances of a particular object.
In the other case you are counting how many objects are equivalent to the object being tested.

It is not a case of efficiency; you can get different results from each.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!