ListIterator 接口的 set() 方法用於替換由 next() 或 previous() 返回的最後一個元素以及給定的元素。隻有當 remove() 和 add(E) 方法都沒有被調用時,才能添加調用。
用法
void set(E e)
參數
上述方法隻需要一個參數:
- 需要替換的元素 'e'。
返回
NA
拋出:
UnsupportedOperationException- 如果列表迭代器不支持給定的集合操作。
ClassCastException- 如果列表迭代器不支持指定元素的給定類。
IllegalArgumentException- 如果給定元素的某些方麵避免將其添加到列表中。
IllegalStateException- 如果 next() 和 previous() 方法都沒有被調用。
例子1
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorsetExample1 {
public static void main(String[] args) {
ArrayList <String> name = new ArrayList<>();
name.add("Ravi");
name.add("Tina");
name.add("Payal");
name.add("Aashi");
System.out.println("The list of the names of the students is given as:"+name);
System.out.println("Before using the set() method:");
ListIterator<String> iterator = name.listIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
iterator.set("None");
System.out.println("After using the set() method:");
for (String item:name) {
System.out.println(item);
}
}
}
輸出:
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
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorsetExample2 {
public static void main(String[] args) {
ArrayList <Float> obj = new ArrayList<>();
obj.add(44.7f);
obj.add(67f);
obj.add(78f);
obj.add(98.9f);
System.out.println("The list of marks of the students is given as:"+obj);
System.out.println("Before using the set() method:");
ListIterator<Float> iterator = obj.listIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
iterator.set(12.4f);
System.out.println("After using the set() method:");
for (Float item:obj) {
System.out.println(item);
}
}
}
輸出:
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
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorsetExample3 {
public static void main(String[] args) {
ArrayList <Long> sub = new ArrayList<>();
sub.add(23l);
sub.add(56l);
sub.add(34l);
sub.add(98l);
System.out.println("The list of numbers of the students is given as:"+sub);
System.out.println("Before using the set() method:");
ListIterator<Long> iterator = sub.listIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
iterator.set(12l);
System.out.println("After using the set() method:");
for (Long item:sub) {
System.out.println(item);
}
}
}
輸出:
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 nextIndex()用法及代碼示例
- Java ListIterator add()用法及代碼示例
- Java ListIterator next()用法及代碼示例
- Java ListIterator hasPrevious()用法及代碼示例
- Java ListIterator remove()用法及代碼示例
- Java ListIterator previousIndex()用法及代碼示例
- Java ListIterator previous()用法及代碼示例
- Java ListIterator hasNext()用法及代碼示例
- Java List spliterator()用法及代碼示例
- Java List size()用法及代碼示例
- Java ListResourceBundle getKeys()用法及代碼示例
- Java List retainAll()用法及代碼示例
- Java List add(E ele)用法及代碼示例
- Java List add()用法及代碼示例
- Java List remove(Object obj)用法及代碼示例
- Java List get()用法及代碼示例
- Java List add(int index, E element)用法及代碼示例
- Java List removeAll()用法及代碼示例
- Java List listIterator()用法及代碼示例
- Java List toArray()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ListIterator set() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。