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