【发布时间】:2016-03-05 21:43:33
【问题描述】:
- 如果我在列表中搜索某个元素但没有找到, 我抛出一个 NotInListException
- 否则我想将其添加到另一个列表中
我做到了
try {
element = actualList.find("foo");
anotherList.append(element);
}
catch (NotInListException e) {
}
这种用法好吗?或者我应该像这样重构它:
if ((element = actualList.find("foo")) != null) {
anotherList.append(element);
}
【问题讨论】:
-
我更喜欢
if (!actualList.contains(element)) anotherList.append(element); -
谢谢,这看起来更“自然”。
-
没有实现 List 我知道有
find()方法,什么是NotInListException?这段代码到底是什么?