09 Jul Vectors in Java
Vectors are growable arrays that need to be initialized before it is used. Vector is a Java class and to use it, import the java.util package like this,
import java.util.*;
The size is to be mentioned in Java Arrays before being used, and once set, it cannot be changed, unlike Vectors. Therefore, Vectors implement a dynamic array.
Create and Declare Vectors
We can declare a vector using any of the following two ways:
- Vector<String> v = new Vector<String>(); ✅ Recommended
- Generics are used here.
- This means the Vector will only store String objects. We can create for other types as well.
- Type safety is enforced at compile time. For example, if you try to add an Integer, the compiler will throw an error.
- Vector v = new Vector(); ❎ Discouraged
- Raw type (no generics).
- This means the Vector can store objects of any type (Strings, Integers, custom objects, etc.).
- Type safety is not enforced at compile time. You could accidentally mix types.
The following are the methods provided by Vectors,
Methods
Let us learn about the methods in Vectors,
| Method | Description |
|---|---|
| void addElement(Object elements) | Add an element to the vector. |
| void insertElementAt(Object element, int index) | Insert an element at a specified index. |
| boolean removeElement(Object element) | Remove the first occurrence of the element. |
| void removeElementAt(int index) | Remove an element from a given index. |
| void removeAllElements() | Remove all the elements. Empties the vector and the size is set to 0. |
| Object firstElement() | Returns the first element. |
| Object lastElement() | Returns the last element. |
| Object ElementAt(int index) | Returns the element at a specified index. |
| int indexOf(Object element) | Returns the index of the first occurrence of the element |
| int lastIndexOf(Object element) | Returns the index of the last occurrence of the element |
| boolean contains(Object a[]) | Returns true if the vector has elements. |
| boolean isEmpty() | True is returned if the vector is empty. |
| Object clone() | Returns the clone of the vector. |
| int capacity() | Returns the capacity of the vector. |
| int size() | Returns the count of elements in the vector |
| void trimToSize() | Sets capacity to the elements it currently holds |
| void setsize(int s) | Set a new size for the vector. |
Now, let us see an example to understand the working of Vectors in Java.
Example – Create a Vector and perform operations
// Create a Vector
import java.util.Vector;
class Demo70 {
static void main(String[] args) {
Vector v = new Vector();
System.out.println(v.size());
Vector subjects = new Vector(10);
System.out.println(subjects.size());
subjects.addElement("PHP");
subjects.addElement("Maths");
subjects.addElement("Java");
subjects.addElement("English");
subjects.addElement("Science");
subjects.addElement("IT");
subjects.addElement("DS");
subjects.addElement("Algorithms");
subjects.addElement("Physics");
subjects.addElement("AI");
subjects.addElement("TOC");
// Displaying in a line
System.out.println(subjects);
System.out.println(subjects.size());
for (int i = 0; i < subjects.size(); i++) {
System.out.println("Elements at index "+i+" = "+subjects.elementAt(i));
}
// Fetching a specific element using an index
System.out.println(subjects.elementAt(5));
// Insert an element at a specific index
subjects.insertElementAt("GenAI", 2);
System.out.println("Updated Vector...");
for (int i = 0; i < subjects.size(); i++) {
System.out.println("Elements at index "+i+" = "+subjects.elementAt(i));
}
System.out.println(subjects.size());
// Find a specific element
System.out.println("Does your vector has subject Algorithms? = "+subjects.contains("Algorithms"));
// Removing elements
subjects.removeElementAt(2);
subjects.removeElementAt(5);
subjects.removeElementAt(7);
subjects.removeElementAt(8);
System.out.println("Updated Vector...");
for (int i = 0; i < subjects.size(); i++) {
System.out.println("Elements at index "+i+" = "+subjects.elementAt(i));
}
System.out.println(subjects.size());
}
}
The following is the output,
0 0 [PHP, Maths, Java, English, Science, IT, DS, Algorithms, Physics, AI, TOC] 11 Elements at index 0 = PHP Elements at index 1 = Maths Elements at index 2 = Java Elements at index 3 = English Elements at index 4 = Science Elements at index 5 = IT Elements at index 6 = DS Elements at index 7 = Algorithms Elements at index 8 = Physics Elements at index 9 = AI Elements at index 10 = TOC IT Updated Vector... Elements at index 0 = PHP Elements at index 1 = Maths Elements at index 2 = GenAI Elements at index 3 = Java Elements at index 4 = English Elements at index 5 = Science Elements at index 6 = IT Elements at index 7 = DS Elements at index 8 = Algorithms Elements at index 9 = Physics Elements at index 10 = AI Elements at index 11 = TOC 12 Does your vector has subject Algorithms? = true Updated Vector... Elements at index 0 = PHP Elements at index 1 = Maths Elements at index 2 = Java Elements at index 3 = English Elements at index 4 = Science Elements at index 5 = DS Elements at index 6 = Algorithms Elements at index 7 = AI 8
In this lesson, we learned about Vectors and the usage of its methods.
No Comments