Vector Class in Java
Vector class implements List
interface so vector class is also growable in nature which internally resizes
array when the elements gets added or removed.
Features of Vector class
Remaining features are similar
to ArrayList class except
1] Vector class is thread safe or you can say it is synchronized in nature.
2] Since JDK 1.0 Vector class is
present in java so it will contains many legacy feature available like Vector class used Enumeration interface
methods.
Vector class Declaration
public class Vector
public class Vector
extends AbstractList
implements List, RandomAccess, Cloneable, java.io.Serializable
Ways to create vector class object / Initial
capacity of vector class
Note: By default vector double its
size.
1] Default Vector class
capacity
Vector vector = new Vector();
public Vector()
{
this(10);
}
It creates an empty Vector with the default initial capacity of 10. It
means the Vector will be re-sized when the 11th elements needs to be inserted
into the Vector. And it will resize its capacity to 20.
2] Constructs an
empty vector with the specified initial capacity
We can create vector class object by providing initialCapacity.
Vector vector = new Vector(4);
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
When we add 5th element in vector class object then it will automatically resize its capacity to double of original Example: It will create 8 as new capacity.
3] Constructs an empty
vector with the specified initial capacity and capacity increment.
Vector vector = new Vector<>(initialCapacity,
capacityIncrement)
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new
IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
initialCapacity - the initial capacity of the
vector
capacityIncrement - the amount by which the
capacity is increased when the vector overflows
Vector class Example
package com.vr.techfloaters;
import java.util.Vector;
public class
VectorExample {
public static void main(String[] args) {
/* Vector of
default capacity(size) of 10 */
Vector v1 = new Vector<>();
/* Vector of
initial capacity(size) of 3 and capacityIncrement is 5*/
Vector v2 = new Vector<>(3, 5);
/* Vector of
initial capacity(size) of 4 */
Vector vector = new Vector<>(4);
/* Adding
elements to a vector*/
vector.add(101);
vector.add("java");
vector.add(null);
vector.add(null);
vector.add(102);
vector.add(103);
vector.add(103);
/* check size and
capacityIncrement*/
System.out.println(vector);
System.out.println("Size is: "+vector.size());
System.out.println("Default capacity increment is:
"+vector.capacity());
System.out.println("--------------------------");
vector.addElement(201);
vector.addElement("ashok");
System.out.println(vector);
System.out.println("Size after addElement is: "+vector.size());
System.out.println("Default capacity after addElement is: "+vector.capacity());
}
}
Output
[101, java,
null, null, 102, 103, 103]
Size is: 7
Default
capacity increment is: 8
--------------------------
[101, java,
null, null, 102, 103, 103, 201, ashok]
Size after
addElement is: 9
Default
capacity after addElement is: 16
0 comments:
Post a Comment