由买买提看人间百态

topics

全部话题 - 话题: getlastfoo
(共0页)
n**********2
发帖数: 214
1
来自主题: JobHunting版 - IPSoft的面试题,和大家共享下
很简单,呵呵。写一个函数的具体实现。
/**
* Given a list of objects, returns the last object in the list that is an
instance of type Foo.
* @param stuff the list of objects
* @return an object of type Foo
* @throws NoSuchFooException if there is no Foo in the list
*/
Foo getLastFoo(List stuff) throws NoSuchFooException;
n**********2
发帖数: 214
2
来自主题: JobHunting版 - IPSoft的面试题,和大家共享下
答案:
Foo getLastFoo(List stuff) throws NoSuchFooException {
for (int i=stuff.size()-1; i>=0; i--) {
if (stuff.get(i) instanceof Foo) {
return (Foo) stuff.get(i);
}
}
throw new NoSuchFooException();
}
(共0页)