如果數組列表爲空,此代碼將返回錯誤。我需要添加代碼以避免錯誤,如果爲空則返回null。由於需要添加代碼才能返回空數組爲空
public Comment findMostHelpfulComment()
{
Iterator<Comment> it = comments.iterator();
Comment best = it.next();
while(it.hasNext())
{
Comment current = it.next();
if(current.getVoteCount() > best.getVoteCount()) {
best = current;
}
}
return best;
}
歡迎來到SO!不幸的是,這不是我們對SO的期望類型。閱讀[問]以瞭解我們如何期待答案。 – 2013-02-26 23:36:22