Hi, in this post we will learn about the basic vector operations in Java. Normally we use vector when we don’t the size of an array and maybe the situation is like this that we have an array and it will increase it’s size when the program is running. In this case, we use vector So we can say that vector is similar to an ArrayList with two differences
- Vector is synchronized
- Vector has a lot of legacy method those methods are not in the collections framework.
In Sort and simple say: Vector implements dynamic array.
Here you can see some basic vector operations.
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 |
package com.practice.house; import java.util.Vector; /** * * @author marifurr */ public class JavaVector { public static void main(String[] args) { Vector<String> vct = new Vector<String>(); //adding elements to the end vct.add("First"); vct.add("Second"); vct.add("Third"); System.out.println(vct); //adding element at specified index vct.add(2, "Random"); System.out.println(vct); //getting elements by index System.out.println("Element at index 3 is: " + vct.get(3)); //getting first element System.out.println("The first element of this vector is: " + vct.firstElement()); //getting last element System.out.println("The last element of this vector is: " + vct.lastElement()); //how to check vector is empty or not System.out.println("Is this vector empty? " + vct.isEmpty()); } } |
Sample Output:
Thanks for reading this post. if you have any question or any suggestion please write me in below. I will be in touch with you.

Java – PHP – Python – Dart – Flutter – MySql – Spring – Hibernate – JavaScript – jQuery – BootStrap 4 – CSS – HTML
I love to explore new technologies. If you like my tutorials please share your thoughts about my work.
Check out my YouTube Channel For Video Tutorials.
To Hire Me Please Contact Me Through Social Media or https:www.amialif.com
If you want to help others by doing donation please Help Them.
You also can connect with me via social media. Thanks
Leave a Reply