/**
* Returns the foo with the matching id in this list
*
* @param id the id of the foo to return
* @return the foo with the matching id in this list
*/
public Foo getFoo(int id)
{
for (Foo foo : list)
{
if (foo.getID() == id)
{
return foo;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
而不是返回的null时候foo没有发现,我应该throw的exception?这是否重要,是否有关于这个问题的"最佳实践"成语?顺便说一下,我知道我的例子有点做作,但我希望你明白这个想法......
谢谢.
编辑
更改了Foo基于id的代码,以更好地说明真实场景.