java.util.Vector.removeRange(int fromIndex,int toIndex) 方法示例


java.util.Vector.removeRange(int fromIndex,int toIndex) 方法示例

package com.codingdict;

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);
      }           
   }    
}