Vectors in Java – The Dynamic Arrays
The vector class of Java
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; that allow adding or removing elements after creation. For that purpose, you may use vectors in Java that implement dynamic arrays.
Before going into further details, have a look at the examples of using the vector class in Java programs.
An example of vector Java 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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:
An example of integer type vector class
In the following example, the vector of integer elements is created. Five elements are added in 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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:
More about vectors
The vectors are synchronized unlike the ArrayList (that are also dynamic). The vectors use capacity and capacityIncrement for optimizing the storage management. The capacity, which is as large as the vector size is generally larger because the vector size increase in chunks as components are added to the vector.
Also, from Java 2 platform, the vector class implements the List interface. The ArrayList also implements the List interface; however, as mentioned earlier, ArrayList is not synchronized. You should prefer using the vector class if the thread-safe implementation is required. Otherwise, prefer using the ArrayList.
See the following section for demos and code of vector including methods provided by the vector Java class, but first, have a look at how you may declare and access vector elements.
How to declare vector?
Following is the syntax for creating a vector in Java program:
Vector<Integer> vector_name = new Vector<Integer>();
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.
Vector<Integer> vector_name = new Vector<Integer>(5);
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 vector from other collection type
You may also create a vector by using the other collection type like ArrayList. Following is the syntax:
Vector vector_name = new Vector (Collection c) ;
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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:
A demo of adding elements at specific position by add method
In 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 demo and code below:
Java code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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:
You saw it returned 4 as the size of the vector.
An example of accessing specific element of vector
By using the get() method, you may access the specific element of the vector. See a demonstration below:
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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 returned its value which is List in above example.