Java ListIterator set() 方法

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

ListIterator 接口的 set() 方法用于使用给定元素替换由 next() 或 previous() 返回的最后一个元素。只有在 neither remove() nor add(E) 方法未被调用时才能调用此方法。

语法

参数

上述方法只需要一个参数

  1. 需要替换的元素 'e'。

返回

不适用

抛出

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

ClassCastException- 如果指定元素的类不被列表迭代器支持。

IllegalArgumentException- 如果给定元素的某些方面阻止其被添加到列表中。

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

示例 1

输出

The list of the names of the students is given as: [Ravi, Tina, Payal, Aashi]
Before using the set() method : 
Ravi
Tina
Payal
Aashi
After using the set() method : 
Ravi
Tina
Payal
None

示例 2

输出

The list of marks of the students is given as: [44.7, 67.0, 78.0, 98.9]
Before using the set() method : 
44.7
67.0
78.0
98.9
After using the set() method : 
44.7
67.0
78.0
12.4

示例 3

输出

The list of age of the students is given as: [23, 56, 34, 98]
Before using the set() method : 
23
56
34
98
After using the set() method : 
23
56
34
12
下一主题Java ListIterator