Java的Vector removeAllElements()方法及示例

Java的Vector removeAllElements()方法及示例

Java.util.Vector.removeAllElements() 方法用于从此Vector中删除所有组件并将其大小设置为零。

语法:

Vector.removeAllElements()

参数: 此方法不需要任何参数。

返回值: 该函数不返回任何值。下面的程序说明了Java.util.Vector.removeAllElements()方法。

例1:

//演示removeAllElements()方法的Java代码
import java.util.*;
 
public class GFG {
   public static void main(String args[]) {
       //创建一个空的Vector
       Vector<String> vector = new Vector<String>();
       
       //使用add()方法将元素添加到Vector中
       vector.add("欢迎");
       vector.add("来到");
       vector.add("Geeks");
       vector.add("4");
       vector.add("Geeks");
  
       //显示向量
       System.out.println("向量:" + vector);
  
       //使用removeAllElements()方法删除向量
       vector.removeAllElements();
  
       //在removeAllElement之后显示最终向量
       System.out.println("最终向量:" + vector);
   }
}

输出:

向量:[欢迎,来到,Geeks,4,Geeks]
最终向量:[]

例2:

//演示removeAllElements()方法的Java代码
import java.util.*;
 
public class GFG {
   public static void main(String args[]) {
       //创建一个空的Vector
       Vector<Integer> vector = new Vector<Integer>();
       
       //使用add()方法将元素添加到Vector中
       vector.add(10);
       vector.add(15);
       vector.add(30);
       vector.add(20);
       vector.add(5);
  
       //显示向量
       System.out.println("向量:" + vector);
  
       //使用removeAllElements()方法删除向量
       vector.removeAllElements();
  
       //在removeAllElement之后显示最终向量
       System.out.println("最终向量:" + vector);
   }
}

输出:

向量:[10, 15, 30, 20, 5]
最终向量:[]

时间复杂度: O(n)。 // n是向量的大小。

辅助空间: O(1)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程