Size and capacity
int capacity()
- Returns the current capacity of this vector.
void ensureCapacity(int minCapacity)
- Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
void setSize(int newSize)
- Sets the size of this vector.
int size()
- Returns the number of components in this vector.
void trimToSize()
- Trims the capacity of this vector to be the vector's current size.
import java.util.Vector;
public class Main{
public static void main(String args[]) {
Vector v = new Vector(2000);
System.out.println(v.capacity());
}
}
The output:
2000
import java.util.Vector;
public class Main{
public static void main(String args[]) {
Vector v = new Vector(2000);
System.out.println(v.capacity());
v.add("java2s.com");
v.trimToSize();
System.out.println(v.capacity());
}
}
The output:
2000
1
Set size of Vector
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("java2s.com");
v.setSize(3);
System.out.println(v);
v.setSize(5);
System.out.println(v);
}
}
The output:
[1, 2, 3]
[1, 2, 3, null, null]
Home
Java Book
Collection
Java Book
Collection
Vector:
- Vector class
- Create Vector objects
- Add elements to this vector
- Size and capacity
- Remove all elements from a vector
- Clone a vector
- Does it contain a certain element
- Copy elements in vector to an array
- Get Enumeration from this vector
- Compare two vectors
- Get element from vector
- Is vector empty
- Get element index
- Remove element from a vector
- Retain elements collection c has
- Replace element in a vector
- Get a sub list from a list
- Convert Vector to Array