java.util.Vector.removeRange()方法实例
removeRange(int fromIndex,int toIndex) 方法用于删除列表索引fromIndex(包括)和为toIndex(不包括)之间的所有元素。
声明
以下是java.util.Vector.removeRange()方法的声明
protected void removeRange(int fromIndex,int toIndex)
参数
-
fromIndex --这是要移除的第一个元素的索引。
-
toIndex --这是要去除最后一个元素之后的索引
返回值
NA
异常
-
NA
例子
下面的例子显示java.util.Vector.removeRange()方法的使用。
package com.yiibai; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 7 Vectorvec = new Vector (7); // use add() method to add elements in the vector vec.add(1); vec.add(2); vec.add(3); vec.add(4); vec.add(5); vec.add(6); vec.add(7); /** * Removing range of elements is not directly supported. However, it * can be done by using subList and clear methods. */ System.out.println("Remove elements from 2nd to 4th"); vec.subList(2,4).clear(); // let us print all the elements available in vector System.out.println("Numbers after removal :- "); for (Integer number : vec) { System.out.println("Number = " + number); } } }
现在编译和运行上面的代码示例,将产生以下结果。
Remove elements from 2nd to 4th Numbers after removal :- Number = 1 Number = 2 Number = 5 Number = 6 Number = 7