Java vector class
In the arrays chapter, we learned the standard arrays in Java are of fixed size. Once created, the size of the array cannot be changed or you can say, the elements in the standard array cannot be added or removed after creation.
In Java programming, you will need to work with arrays that are dynamic; and that allow adding or removing elements after creation.
For that purpose, you may use vectors in Java that implement dynamic arrays.
Vector<String> vectFruits = new Vector<String>(); //Adding elements vectFruits.add("Apple"); vectFruits.add("Mango"); vectFruits.add("Banana"); //Remove elements vectFruits.remove(0); //Access String element = vectFruits.get(1);
- Vectors implement the List interface
- Ordered collections of elements
- Vectors dynamically resize themselves
- Allow for random access, insertion, and deletion of elements.
- Vectors are synchronized
- Vectors are thread-safe
An example of a Java vector class with strings
Before describing how to declare and access vectors, let me show you a few examples of using this in Java programs.
In this example, a vector of string elements is created. Three elements are added by using the add method after vector declaration. This is followed by using the for loop for displaying the elements:
The Java code with vector class:
import java.util.*; public class vector_demo { public static void main(String []args) { Vector<String> vectStr = new Vector<String>(); //Declaring a Vector in Java vectStr.add("The"); vectStr.add("Vector"); vectStr.add("Tutorial"); //Displaying vector elements System.out.println("The Vector elements are:"); System.out.println(""); for (int i=0;i<vectStr.size();i++){ System.out.println(vectStr.get(i)); } } }
Output:
The
Vector
Tutorial
An example of integer-type vector class
In the following example, the vector of integer elements is created. Five elements are added to the vector after declaring it by using the add method.
The for-each loop is used for displaying the vector items as shown below:
The code:
import java.util.*; public class vector_demo { public static void main(String []args) { Vector<Integer> vectInt = new Vector<Integer>(); //Declaring a Vector in Java vectInt.add(111); vectInt.add(222); vectInt.add(333); vectInt.add(444); vectInt.add(555); //Displaying vector elements System.out.println("The Integer Vector elements are:"); System.out.println(""); for (int currVectorElement : vectInt) { System.out.println(currVectorElement); } } }
Output:
222
333
444
555
More about vectors
- The vectors are synchronized unlike the ArrayList (that are also dynamic).
- The vectors use capacity and capacityIncrement for optimizing storage management. The capacity, which is as large as the vector size is generally larger because the vector size increases in chunks as components are added to the vector.
- Also, from the Java 2 platform, the vector class implements the List interface.
- The ArrayList also implements the List interface; however, as mentioned earlier, the ArrayList is not synchronized.
- You should prefer using the vector class if the thread-safe implementation is required. Otherwise, prefer using the ArrayList.
How to declare a vector?
Following is the syntax for creating a vector in a Java program:
The Vector class is followed by the type and vector name on the left side. On the right side, the new keyword is used which is followed by Vector class. Optionally, you may specify the size of vector, e.g.
If you do not specify the data type at the time of declaration, the vector may contain different types of elements like Strings, integers etc.
Creating vectors from another collection type
You may also create a vector by using the other collection type like ArrayList. Following is the syntax:
See the following example where I have created an ArrayList with five elements. This is followed by creating a vector where that ArrayList object is specified. Finally, a for loop is used for displaying the elements of that vector:
Code for creating a vector from an ArrayList:
import java.util.*; public class vector_demo { public static void main(String []args) { ArrayList<String> arrlst1 = new ArrayList<String>(); //Declaring ArrayList arrlst1.add("Creating"); arrlst1.add("Vector"); arrlst1.add("From"); arrlst1.add("ArrayList"); Vector<String> Vect_from_ArrayList = new Vector<String>(arrlst1); //Declaring Vector from ArrayList //Displaying vector elements System.out.println("The vector elements after creating from ArrayList:"); System.out.println(" "); for (int i=0;i<Vect_from_ArrayList.size();i++){ System.out.println("Element No "+i +": " +Vect_from_ArrayList.get(i)); } } }
Output:
Element No 0: Creating
Element No 1: Vector
Element No 2: From
Element No 3: ArrayList
A demo of adding elements at specific position by add method
In the above examples, the elements in the vector are added by using the add method in sequence.
The add method allows adding the elements at the specific position as well. For that, provide the index number and elements to be added in brackets separated by a comma. See the code and output below:
Java code:
import java.util.*; public class vector_demo { public static void main(String []args) { Vector<Integer> vectInt = new Vector<Integer>(); //Declaring a Vector in Java vectInt.add(111); vectInt.add(222); vectInt.add(444); vectInt.add(666); //Displaying vector elements System.out.println("The vector before adding items in specific position:"); System.out.println(""); for (int currVectorElement : vectInt) { System.out.println(currVectorElement); } //Adding new items at specific positions vectInt.add(2,333); vectInt.add(4,555); //Displaying vector elements System.out.println("The vector after adding items in specific position:"); System.out.println(""); for (int currVectorElement : vectInt) { System.out.println(currVectorElement); } } }
Result:
111
222
444
666
The vector after adding items in specific position:
111
222
333
444
555
666
Getting the length of vector by using size method
The size method returns the total number of elements in the specified vector. See the demo below where I have used the size method twice.
One for displaying the total elements in the vector. The other is used in the for loop for specifying the maximum number of times the for loop should run:
The code:
import java.util.*; public class vector_demo { public static void main(String []args) { Vector<String> vectSize = new Vector<String>(); //Declaring a Vector in Java vectSize.add("The"); vectSize.add("Vectors"); vectSize.add("are"); vectSize.add("Synchronized"); //Displaying vector elements System.out.println("The size of the Vector: " + vectSize.size()); System.out.println(""); for (int i=0;i< vectSize.size();i++){ System.out.println(vectSize.get(i)); } } }
Output:
Vectors
are
Synchronized
You saw it returned 4 as the size of the vector.
An example of accessing a specific element of vector
By using the get() method, you may access the specific element of the vector. See a demonstration below:
The code:
import java.util.*; public class vector_demo { public static void main(String []args) { Vector<String> vectSize = new Vector<String>(); //Declaring a Vector in Java vectSize.add("The"); vectSize.add("Vectors"); vectSize.add("implements"); vectSize.add("List"); vectSize.add("interface"); //Displaying vector elements System.out.println("The vector element at index 3 = " + vectSize.get(3)); } }
Output:
The element index is given in the get method and it returns its value which is List in the above example.