Java ListIterator remove() 方法

2025 年 3 月 22 日 | 阅读需 2 分钟

ListIterator 接口的 remove() 方法用于从列表中移除由 next() 或 previous() 方法返回的最后一个元素。只有在没有调用过 add(E) 方法的情况下,才能调用上述方法。

语法

参数

不适用

指定者

在接口 Iterator<E> 中的 remove

返回

不适用

抛出

UnsupportedOperationException - 如果此列表迭代器不支持给定的 remove 操作。

IllegalStateException - 如果 next() 或 previous() 方法都未被调用。

示例 1

输出

The list for the vehicles is given as :[Car, Truck, Bike, Cycle]
Before remove() method is called. : [Car, Truck, Bike, Cycle]
After remove() method is called.: [Car, Bike, Cycle]

示例 2

输出

The list for the marks is given as :[55.0, 69.0, 78.0, 89.0]
Before remove() method is called. : [55.0, 69.0, 78.0, 89.0]
After remove() method is called.: [55.0, 78.0, 89.0]

示例 3

输出

The list for the ages is given as :[25, 30, 35, 40, 45]
Before remove() method is called. : [25, 30, 35, 40, 45]
After remove() method is called.: [25, 35, 40, 45]
下一主题Java ListIterator