removeAllElements() 方法在 java 中有什么作用?


removeAllElements() 方法用于移除此向量的所有分量,并将其大小设为零。此方法的功能与 clear 方法相同。

示例

import java.util.Vector;
public class VectorDemo {
   public static void main(String[] args) {

      Vector<Integer> vec = new Vector<Integer>(4);
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);
      System.out.println("Added numbers are :- ");
      for (Integer number : vec) {
         System.out.println("Number = " + number);
      }
      System.out.println("Size of the vector: "+vec.size());
      System.out.println("Removing all elements");
      vec.removeAllElements();
      System.out.println("Now size of the vector: "+vec.size());
   }
}

输出

Added numbers are :-
Number = 4
Number = 3
Number = 2
Number = 1
Size of the vector: 4
Removing all elements
Now size of the vector: 0

更新于:2020-2-25

106 次浏览

开启您的 职业生涯

完成课程认证

开始学习
广告