ListIterator 接口的 remove() 方法用於從 next() 或 previous() 方法返回的列表中刪除最後一個元素。隻有當 add(E) 沒有被調用時才能調用上述方法。
用法
void remove()
參數
NA
指定者:
在接口 Iterator<E> 中刪除
返回
NA
拋出:
UnsupportedOperationException- 如果列表迭代器不支持給定的刪除操作。
IllegalStateException- 如果 next() 和 previous() 方法都沒有被調用。
例子1
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorremoveExample1 {
public static void main(String[] args) {
ArrayList < String > vehicles = new ArrayList < String > ();
vehicles.add("Car");
vehicles.add("Truck");
vehicles.add("Bike");
vehicles.add("Cycle");
System.out.println("The list for the vehicles is given as:"+vehicles);
ListIterator < String > listIterator = vehicles.listIterator();
System.out.println("Before remove() method is called.:" + vehicles);
listIterator.next();
listIterator.next();
listIterator.remove();
System.out.println("After remove() method is called.:" + vehicles);
}
}
輸出:
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
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorremoveExample2 {
public static void main(String[] args) {
ArrayList < Float > marks = new ArrayList < Float > ();
marks.add(55f);
marks.add(69f);
marks.add(78f);
marks.add(89f);
System.out.println("The list for the marks is given as:"+marks);
ListIterator < Float > listIterator = marks.listIterator();
System.out.println("Before remove() method is called.:" + marks);
listIterator.next();
listIterator.next();
listIterator.remove();
System.out.println("After remove() method is called.:" + marks);
}
}
輸出:
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
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorremoveExample3 {
public static void main(String[] args) {
ArrayList < Integer > marks = new ArrayList < Integer > ();
marks.add(25);
marks.add(30);
marks.add(35);
marks.add(40);
marks.add(45);
System.out.println("The list for the ages is given as:"+marks);
ListIterator < Integer > listIterator = marks.listIterator();
System.out.println("Before remove() method is called.:" + marks);
listIterator.next();
listIterator.next();
listIterator.remove();
listIterator.next();
System.out.println("After remove() method is called.:" + marks);
}
}
輸出:
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 nextIndex()用法及代碼示例
- Java ListIterator add()用法及代碼示例
- Java ListIterator next()用法及代碼示例
- Java ListIterator hasPrevious()用法及代碼示例
- Java ListIterator previousIndex()用法及代碼示例
- Java ListIterator set()用法及代碼示例
- 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 remove() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。