Add elements in Vector

We can use add() method of Vector to insert new elements to Vector

There are 5 different “add” methods defined in Vector


1) boolean add(Object o);
2) void add(int index, Object o);
3) boolean addAll(Collection c);
4) boolean addAll(int index,Collection c);
5) void addElement(Object o);


Let’s discuss each of these methods with example

boolean add(Object o)

This method inserts the specified element to the end of the vector.

Example :

  1. import java.util.*;
  2. public class VectorAdd {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add(“Java”);
  6. vector.add(“c++);
  7. vector.add(“python”);
  8.     System.out.println(vector);
  9.     }
  10. }
import java.util.*;
public class VectorAdd {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add(“Java”);
vector.add(“c++”);
vector.add(“python”);
	System.out.println(vector);
	}
}
Note : We can see each element is added at the end of the vector

void add(int index, Object o)

This method Inserts the specified element at the specified position in the vector.

Example :

  1. import java.util.*;
  2. public class VectorAddAtIndex {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("Java");
  6. vector.add("c++");
  7. vector.add("python");
  8. vector.add(1,"c");
  9.     System.out.println(vector);
  10.     }
  11. }
import java.util.*;
public class VectorAddAtIndex {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("Java");
vector.add("c++");
vector.add("python");
vector.add(1,"c");
	System.out.println(vector);
	}
}
Note : We can see that element “c” is added at 1st index

boolean addAll(Collection c)

This method appends all of the elements in the specified collection to the end of this vector, in the order that they are returned by the specified collection’s Iterator

Example :

  1. import java.util.*;
  2. public class VectorAddCollection {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("Java");
  6.         vector.add("c++");
  7.         Vector<String> vector2 = new Vector<String>();
  8.         vector2.add("python");
  9.         vector2.add("c");
  10.         vector.addAll(vector2);
  11.     System.out.println(vector);
  12.     }
  13. }
import java.util.*;
public class VectorAddCollection {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("Java");
        vector.add("c++");
        Vector<String> vector2 = new Vector<String>();
        vector2.add("python");
        vector2.add("c");
        vector.addAll(vector2);
	System.out.println(vector);
	}
}
Note : We can see that we have added another collection “vector2” to “vector” using “addAll” method and all the 
           
            elements of “vector2” are added at the end of “vector”

boolean addAll(int index,Collection c)

This method Inserts all of the elements in the specified collection into the list, starting at the specified position.

Example :

  1. import java.util.*;
  2. public class VectorAddCollectionAtIndex {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("Java");
  6. vector.add("c++");
  7. Vector<String> vector2 = new Vector<String>();
  8. Vector2.add("python");
  9. Vector2.add("c");
  10. vector.addAll(1,vector2);
  11.     System.out.println(vector);
  12.     }
  13. }
import java.util.*;
public class VectorAddCollectionAtIndex {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("Java");
vector.add("c++");
Vector<String> vector2 = new Vector<String>();
Vector2.add("python");
Vector2.add("c");
vector.addAll(1,vector2);
	System.out.println(vector);
	}
}
Note : We can see that we have added another collection “vector2” to “vector” using “addAll” method at specified 
            
            index “1” and all the elements of “vector2” are added at the index “1” of “vector”

void addElement(Object o)

This method add the element to the vector

Example :

  1. import java.util.*;
  2. public class VectorAddElement {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector= new Vector<String>();
  5.     vector.add("Java");
  6. vector.add("c++");
  7. vector.addElement("c");
  8.     System.out.println(vector);
  9.     }
  10. }
import java.util.*;
public class VectorAddElement {
	public static void main(String[] args) {
	Vector<String> vector= new Vector<String>();
	vector.add("Java");
vector.add("c++");
vector.addElement("c");
	System.out.println(vector);
	}
}

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on