当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ListIterator remove()用法及代码示例


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