Java Vector size()
Java Vector类的
size()方法用于获取Vector 中存在的组件数。
语法
以下是
size()方法的声明:
参数
此方法不接受任何参数。
返回
大小()方法返回向量中存在的组件数。
异常
NA
兼容性版本
Java 1.2及更高版本
示例1
import java.util.*;
public class VectorSizeExample1 {
public static void main(String arg[]) {
//Create an empty vector
Vector<Integer> vec = new Vector<>();
//Add element in the vector
vec.add(10);
vec.add(20);
vec.add(30);
vec.add(40);
vec.add(50);
//Displaying the vector element
System.out.println("The vector Element is = "+vec);
//Get the number of components in the vector
System.out.println("Size of the vector is = "+vec.size());
}
}
输出:
The vector Element is = [10, 20, 30, 40, 50]
Size of the vector is = 5
示例2
import java.util.*;
public class VectorSizeExample2 {
public static void main(String arg[]) {
//Create an empty vector
Vector < String > colors = new Vector < String > (10);
//Add elements in the vector
colors.add("White");
colors.add("Green");
colors.add("Black");
colors.add("Pink");
//Get the number of components in the vector
System.out.println("Size of the vector is = "+colors.size());
}
}
输出:
Size of the vector is = 4