Java中的Vector capacity()方法

这个 JAVAutil。矢量。容量() 方法用于获取向量的容量或向量中存在的数组的长度。

null

语法:

Vector.capacity()

参数: 该方法不接受任何参数。

返回值: 该方法返回向量中存在的容量或内部数据数组的长度,这是一个整数值。

下面的程序演示了Java。util。矢量。capacity()方法:

项目1: 带有字符串元素的向量。

// Java code to illustrate capacity()
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
// Creating an empty Vector
Vector<String> vec_tor = new Vector<String>();
// Use add() method to add elements into the Vector
vec_tor.add( "Welcome" );
vec_tor.add( "To" );
vec_tor.add( "Geeks" );
vec_tor.add( "4" );
vec_tor.add( "Geeks" );
// Displaying the Vector
System.out.println( "Vector: " + vec_tor);
// Displaying the capacity of Vector
System.out.println( "The capacity is: " + vec_tor.capacity());
}
}


输出:

Vector: [Welcome, To, Geeks, 4, Geeks]
The capacity is: 10

项目2: 带有整数元素的向量。

// Java code to illustrate capacity()
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
// Creating an empty Vector
Vector<Integer> vec_tor = new Vector<Integer>();
// Use add() method to add elements into the Vector
vec_tor.add( 10 );
vec_tor.add( 15 );
vec_tor.add( 30 );
vec_tor.add( 20 );
vec_tor.add( 5 );
// Displaying the Vector
System.out.println( "Vector: " + vec_tor);
// Displaying the capacity of Vector
System.out.println( "The capacity is: " + vec_tor.capacity());
}
}


输出:

Vector: [10, 15, 30, 20, 5]
The capacity is: 10

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享