当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Vector clear()用法及代码示例


Java.util.Vector.clear()方法用于从Vector中删除所有元素。使用clear()方法仅清除向量中的所有元素,而不会删除向量。换句话说,可以说clear()方法仅用于清空现有矢量。

用法:

Vector.clear()

参数:该方法不带任何参数


返回值:该函数不返回任何值。

以下示例程序旨在说明Java.util.Vector.clear()方法。

示例1:

// Java code to illustrate clear() 
import java.util.*; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Creating an empty Vector 
        Vector<String> vt = new Vector<String>(); 
  
        // Use add() method to add elements into the Vector 
        vt.add("Welcome"); 
        vt.add("To"); 
        vt.add("Geeks"); 
        vt.add("4"); 
        vt.add("Geeks"); 
  
        // Displaying the Vector 
        System.out.println("Vector: " + vt); 
  
        // Clearing the Vector using clear() method 
        vt.clear(); 
  
        // Displaying the final vector after clearing; 
        System.out.println("The final Vector: " + vt); 
    } 
}
输出:
Vector: [Welcome, To, Geeks, 4, Geeks]
The final Vector: []

示例2:

// Java code to illustrate clear() 
import java.util.*; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Creating an empty Vector 
        Vector<Integer> vt = new Vector<Integer>(); 
  
        // Use add() method to add elements into the Queue 
        vt.add(10); 
        vt.add(15); 
        vt.add(30); 
        vt.add(20); 
        vt.add(5); 
  
        // Displaying the Vector 
        System.out.println("Vector: " + vt); 
  
        // Clearing the Vector using clear() method 
        vt.clear(); 
  
        // Displaying the final vector after clearing; 
        System.out.println("The final Vector: " + vt); 
    } 
}
输出:
Vector: [10, 15, 30, 20, 5]
The final Vector: []


相关用法


注:本文由纯净天空筛选整理自kundankumarjha大神的英文原创作品 Vector clear() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。