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: []
相關用法
- Java Map clear()用法及代碼示例
- Java TreeMap clear()用法及代碼示例
- Java PriorityQueue clear()用法及代碼示例
- Java HashSet clear()用法及代碼示例
- Java NavigableSet clear()用法及代碼示例
- Java ArrayDeque clear()用法及代碼示例
- Java LinkedBlockingDeque clear()用法及代碼示例
- Java DelayQueue clear()用法及代碼示例
- Java AbstractSequentialList clear()用法及代碼示例
- Java WeakHashMap clear()用法及代碼示例
- Java ConcurrentSkipListSet clear()用法及代碼示例
- Java CopyOnWriteArraySet clear()用法及代碼示例
- Java Stack clear()用法及代碼示例
- Java NavigableMap clear()用法及代碼示例
- Java LinkedList clear()用法及代碼示例
注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 Vector clear() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。