Java Vector isEmpty()
Java Vector类的
isEmpty()方法用于检查此Vector是否不包含任何组件。如果向量为空,则返回true,否则返回false。
语法
以下是
isEmpty()方法的声明:
参数
此方法不接受任何参数。
返回
isEmpty()方法仅当此向量没有任何分量时返回true,否则返回false。
异常
NA
兼容性版本
Java 1.2及更高版本
示例1
import java.util.*;
public class VectorIsEmptyExample1 {
public static void main(String arg[]) {
//Create an empty vector
Vector < String > vec = new Vector < String > ();
//Add elements in the vector
vec.add("Java");
vec.add("Ruby");
vec.add("Android");
vec.add("Python");
//Displaying the Vector
System.out.println("Vector: " + vec);
//Verifying if the Vector is empty or not
System.out.println("Is the Vector empty? = " +vec.isEmpty());
//Clear the Vector
vec.clear();
//Displaying the Vector Again
System.out.println("Vector after clear(): " +vec);
//Verifying if the Vector is empty or not
System.out.println("Is the Vector empty? = " +vec.isEmpty());
}
}
输出:
Vector: [Java, Ruby, Android, Python]
Is the Vector empty? = false
Vector after clear(): []
Is the Vector empty? = true
示例2
import java.util.*;
public class VectorIsEmptyExample2 {
public static void main(String arg[]) {
//Create an empty Vector with initial capacity of 5
Vector<Integer> vecObject = new Vector<Integer>(5);
//Display the Vector
System.out.println("Vector: " +vecObject);
//Verifying if the Vector is empty or not
System.out.println("Is the Vector empty? = " +vecObject.isEmpty());
}
}
输出:
Vector: []
Is the Vector empty? = true
示例3
import java.util.*;
public class VectorIsEmptyExample3 {
public static void main(String arg[]) {
//Create an empty Vector
Vector < String > colors = new Vector < String > ();
//Add color elements in the vector
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Pink");
//Test the vector if it has no components
System.out.println("Vector isEmpty() before? " +colors.isEmpty());
//Remove all elements from the vector
colors.removeAllElements();
//Again test the vector if it has no components
System.out.println("Vector isEmpty() after ? " +colors.isEmpty());
}
}
输出:
Vector isEmpty() before? false
Vector isEmpty() after ? true