-5

所以我正在查看 TreeSet 类中的 remove() 方法,该方法的返回类型为布尔值。java api 表示,如果我们要删除的项目在树中,并且被删除,则该方法返回 true。如果项目不在树中,或者已经被删除,这个方法会引发异常吗?我可以修改该方法,以便在未删除元素时返回 false 吗?

 remove

 public boolean remove(Object o)
 Removes the specified element from this set if it is present.(...)
 Returns true if this set contained the element (or equivalently, if this set changed as a            result of the call). (This set will not contain the element once the call returns.)
4

1 回答 1

3

您通过阅读文档回答了自己的问题。

如果项目不在树中,或者已经被删除,这个方法会引发异常吗?

然后是文档:

如果此集合包含元素,则返回 true

所以如果集合不包含该元素,它将返回false(“不在树中”和“已被删除”实际上是相同的情况)

于 2012-02-25T20:08:20.983 回答