1

使用迭代时,会IllegalStateException抛出什么条件?我正在做一个任务,它是多项选择:

a) 调用remove后调用next
b) 调用next后调用previous
c) 调用remove后调用remove
d) 调用remove后调用previous

我在“API docs”中发现的是,如果该next方法尚未被调用,或者该remove方法在最后一次调用该方法后已经被调用next

所以答案必须是“a”,但我被告知这是错误的。为什么我的推理是错误的,正确的答案是什么?

4

1 回答 1

2

You have a list [a, b, c, d, e]. A pointer N starts out pointing at nothing. This is the standard starting position for an iterator in Java.

Scenario A) - call next, N is now pointing at a. Call remove, a is gone and the list is [b, c, d, e], N is pointing at nothing.

Scenario B) Call previous, N is now pointing at e. Call next, N is now pointing at a.

Scenario C) Call next, N is pointing at a. Call remove, a is gone, N is pointing at nothing. Call remove,IllegalStateExceptionis thrown.N` is pointing at nothing, so nothing can be removed.

Scenario D) Call previous, N is pointing at e. Call remove, e is gone, N is pointing at nothing.

Scenario E) Call remove, N is pointing at nothing, so IllegalStateException is thrown.

于 2012-04-10T21:00:12.853 回答