Vector is like the dynamic array(re-sizable array) which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit. The Vector class implements a growable array of objects. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here.
It is recommended to use the Vector class in the thread-safe implementation only. If you don't need to use the thread-safe implementation, you should use the ArrayList, the ArrayList will perform better in such case. The java.util.Vector class implements a growable array of objects. Similar to an Array, it contains components that can be accessed using an integer index.
It is similar to the ArrayList, but with two differences- Vector is synchronized.
Java Vector contains many legacy methods that are not the part of a collections framework.
Synchronization in java
Synchronization in java is the capability to control the access of multiple threads to any shared resource. Java Synchronization is better option where we want to allow only one thread to access the shared resource.
Java Vector class Declaration public class Vector<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
Here <E> represents an Element, which could be any class.
For example, if you're building an array list of Integers then you'd initialize it as follows − ArrayList<Integer> list = new ArrayList<Integer>();
Three ways to create vector class object:
Method 1:
Vector vec = new Vector();
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. Note: By default vector doubles its size. i.e. In this case the Vector size would remain 10 till 10 insertions and once we try to insert the 11th element It would become 20 (double of default capacity 10).
Method 2:
Syntax: Vector object= new Vector(int initialCapacity) Vector vec = new Vector(3);
It will create a Vector of initial capacity of 3.
Method 3:
Syntax:
Vector object= new vector(int initialcapacity, capacityIncrement) Example:
Vector vec= new Vector(4, 6)
Here we have provided two arguments. The initial capacity is 4 and capacityIncrement is 6. It means upon insertion of 5th element the size would be 10 (4+6) and on 11th insertion it would be 16(10+6).
Ex:
import java.util.*;
public class VectorExample
{
public static void main(String args[])
{
//Create a vector
Vector<String> vec = new Vector<String>();
//Adding elements using add() method of List vec.add("Tiger");
vec.add("Lion");
vec.add("Dog"); vec.add("Elephant");
//Adding elements using addElement() method of Vector vec.addElement("Rat");
vec.addElement("Cat"); vec.addElement("Deer");
System.out.println("Elements are: "+vec);
}}
Output:
Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]
Ex:
import java.util.*;
public class VectorExample
{
public static void main(String args[])
{
/* Vector of initial capacity(size) of 2 */ Vector<String> vec = new Vector<String>(2);
/* Adding elements to a vector*/ vec.addElement("Apple"); vec.addElement("Orange"); vec.addElement("Mango"); vec.addElement("Fig");
/* check size and capacityIncrement*/ System.out.println("Size is: "+vec.size());
System.out.println("Default capacity increment is: "+vec.capacity());
vec.addElement("fruit1"); vec.addElement("fruit2"); vec.addElement("fruit3");
/*size and capacityIncrement after two insertions*/ System.out.println("Size after addition: "+vec.size()); System.out.println("Capacity after increment is: "+vec.capacity());
/*Display Vector elements*/ Enumeration en = vec.elements(); System.out.println("\nElements are:"); while(en.hasMoreElements())
System.out.print(en.nextElement() + " ");
}
}
Output:
Size is: 4
Default capacity increment is: 4 Size after addition: 7
Capacity after increment is: 8
Elements are:
Apple Orange Mango Fig fruit1 fruit2 fruit3
Ex:
import java.util.*;
public class VectorExample1
{
public static void main(String args[])
{
//Create an empty vector with initial capacity 4 Vector<String> vec = new Vector<String>(4);
//Adding elements to a vector vec.add("Tiger");
vec.add("Lion");
vec.add("Dog"); vec.add("Elephant");
//Check size and capacity System.out.println("Size is: "+vec.size());
System.out.println("Default capacity is: "+vec.capacity());
//Display Vector elements System.out.println("Vector element is: "+vec); vec.addElement("Rat"); vec.addElement("Cat"); vec.addElement("Deer");
//Again check size and capacity after two insertions System.out.println("Size after addition: "+vec.size()); System.out.println("Capacity after addition is: "+vec.capacity());
//Display Vector elements again System.out.println("Elements are: "+vec);
//Checking if Tiger is present or not in this vector if(vec.contains("Tiger"))
{
System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));
}
else
{
System.out.println("Tiger is not present in the list.");
}
//Get the first element
System.out.println("The first animal of the vector is = "+vec.firstElement());
//Get the last element
System.out.println("The last animal of the vector is = "+vec.lastElement());
}
}
Output:
Size is: 4
Default capacity is: 4
Vector element is: [Tiger, Lion, Dog, Elephant]
Size after addition: 7 Capacity after addition is: 8
Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer] Tiger is present at the index 0
The first animal of the vector is = Tiger The last animal of the vector is = Deer
Ex:
import java.util.Vector; class Main2
{
public static void main(String[] args)
{
Vector<String> n= new Vector<>();
// Using the add() method n.add("One");
n.add("Two");
// Using index number n.add(1, "Three");
System.out.println("Vector: " + n);
// Using addAll()
Vector<String> m=new Vector<>(); m.add("Five");
m.addAll(n);
System.out.println("New Vector: " + m); m.add("Ten");
n.addAll(m);
System.out.println("New Vector1: " + n);
}
}
Output:
I:\>javac Main2.java I:\>java Main2
Vector: [One, Three, Two]
New Vector: [Five, One, Three, Two]
New Vector1: [One, Three, Two, Five, One, Three, Two, Ten]
Access Vector Elements
get(index) - returns an element specified by the index
iterator() - returns an iterator object to sequentially access vector elements
Ex:
import java.util.Iterator; import java.util.Vector;
class Main
{
public static void main(String[] args)
{
Vector<String> animals= new Vector<>(); animals.add("Dog"); animals.add("Horse");
animals.add("Cat");
// Using get()
String element = animals.get(2); System.out.println("Element at index 2: " + element);
// Using iterator()
Iterator<String> iterate = animals.iterator(); System.out.print("Vector: "); while(iterate.hasNext()) {
System.out.print(iterate.next()); System.out.print(", ");
}
}
}
Output
Element at index 2: Cat Vector: Dog, Horse, Cat,
Remove Vector Elements
remove(index) - removes an element from specified position removeAll() - removes all the elements
clear() - removes all elements. It is more efficient than removeAll()
Ex:
import java.util.Vector; class Main
{
public static void main(String[] args)
{
Vector<String> animals= new Vector<>(); animals.add("Dog"); animals.add("Horse");
animals.add("Cat");
System.out.println("Initial Vector: " + animals);
// Using remove()
String element = animals.remove(1); System.out.println("Removed Element: " + element); System.out.println("New Vector: " + animals);
// Using clear() animals.clear();
System.out.println("Vector after clear(): " + animals);
}
}
Output
Initial Vector: [Dog, Horse, Cat] Removed Element: Horse
New Vector: [Dog, Cat] Vector after clear(): []
Ex:
import java.util.*;
public class VectorExample2
{
public static void main(String args[])
{
//Create an empty Vector Vector<Integer> in = new Vector<>();
//Add elements in the vector in.add(100);
in.add(200);
in.add(300);
in.add(200);
in.add(400);
in.add(500);
in.add(600);
in.add(700);
//Display the vector elements System.out.println("Values in vector: " +in);
//use remove() method to delete the first occurrence of an element System.out.println("Remove first occurrence of element 200: "+in.remove((Integer)200));
//Display the vector elements afre remove() method System.out.println("Values in vector: " +in);
//Remove the element at index 4
System.out.println("Remove element at index 4: " +in.remove(4)); System.out.println("New Value list in vector: " +in);
//Remove an element in.removeElementAt(5);
//Checking vector and displays the element System.out.println("Vector element after removal: " +in);
//Get the hashcode for this vector
System.out.println("Hash code of this vector = "+in.hashCode());
//Get the element at specified index System.out.println("Element at index 1 is = "+in.get(1));
}
}
Output:
Values in vector: [100, 200, 300, 200, 400, 500, 600, 700]
Remove first occurrence of element 200: true
Values in vector: [100, 300, 200, 400, 500, 600, 700]
Remove element at index 4: 500
New Value list in vector: [100, 300, 200, 400, 600, 700]
Vector element after removal: [100, 300, 200, 400, 600] Hash code of this vector = 130123751
Element at index 1 is = 300
No comments:
Post a Comment