In C#, finding an item in an ArrayList that have a certain property, it’s quite easy:
mSelectedBoard = mBoardConnections.FirstOrDefault(bcd => bcd.Id == id);
This is the easiest I’ve found to do the same in Java (wish I could afford MonoTouch for android):
for ( BoardConnectionData bcd : mBoardConnections ) {
if (bcd.getID() == id) {
mSelectedBoard = bcd;
break;
}
}
Am I missing an easier way to do this?
Thanks!
Define a meaningfulequalsand useCollection.contains.There are a number of utility libraries that contain implementations using various “am I it?” implementations. (Guava, Commons Collections, etc.)
Another option is to create a typed collection containing utility methods finding elements by arbitrary criteria.
In any case, the code snippet shown shouldn’t live in the mainline code, it should be abstracted into its own method, regardless of where it ends up living.