Java教程

Java Vector clear()

Java Vector 类的 clear()方法用于删除所有

语法:

以下是 clear()方法的声明:
Public void clear()

参数:

此方法不接受任何参数。

返回:

此方法具有返回类型为void,因此它不返回任何值。

例外:

NA

兼容性版本:

Java 1.2及更高版本

示例1:

import java.util.Vector;
public class VectorClearExample1 {  
    public static void main(String arg[]) {
        //Create an empty Vector    
          Vector<String> vc = new Vector<>();
          //Add elements in the vector by using add() method
          vc.add("A");
          vc.add("B");
          vc.add("C");   
          //Print the size of vector
          System.out.println("Size of Vector before clear() method: "+vc.size());
          //Clear the vector
          vc.clear();
          System.out.println("Size of Vector after clear() method: "+vc.size());
            }
}
输出:
Size of Vector before clear() method: 3
Size of Vector after clear() method: 0

示例2:

import java.util.Vector;
public class VectorClearExample2 {  
    public static void main(String arg[]) {
        //Create an empty Vector with initial capacity of 5      
        Vector<Integer> vecObject = new Vector<Integer>(5);
        //Add values in the vector
        vecObject.add(3);
        vecObject.add(5);
        vecObject.add(2);
        vecObject.add(4);
        vecObject.add(1);
          System.out.println("Elements of Vector is: "+vecObject);   
          //Clear vector
          vecObject.clear();
          System.out.println("Elements of Vector after clear(): "+vecObject);
            }
}
输出:
Elements of Vector is: [3, 5, 2, 4, 1]
Elements of Vector after clear(): []

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4