java.util.Vector.removeRange()


描述

removeRange(int fromIndex,int toIndex)方法被用来去除从名单其索引之间fromIndex(包括)元素范围为独占的所有元素。

声明

以下是java.util.Vector.removeRange()方法的声明

protected void removeRange(int fromIndex,int toIndex)

参数

fromIndex - 这是要删除的第一个元素的索引。

toIndex - 这是要删除的最后一个元素之后的索引

返回值

NA

异常

NA

实例

以下示例显示了java.util.Vector.removeRange()方法的用法。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 7      
      Vector<Integer> vec = new Vector<Integer>(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