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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。