Java ListIterator set() Method

The set() method of ListIterator interface is used to replace the last element which is returned by the next() or previous() along with the given element. The call can be added only if neither remove() nor add(E) method have been called.

Syntax

Parameters

The above method requires only one parameter:

  1. The element 'e' which needs to be replaced.

Return

NA

Throws:

UnsupportedOperationException- If the given set operation is not supported by the list iterator.

ClassCastException- If the given class of the specified element is not supported by the list iterator.

IllegalArgumentException- If some of the aspects of the given element avoid it from being added to the list.

IllegalStateException- If neither the next() nor the previous() method has been called.

Example 1

Test it Now

Output:

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

Example 2

Test it Now

Output:

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

Example 3

Test it Now

Output:

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




Latest Courses