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 extends E> c);
4) boolean addAll(int index,Collection extends E> 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 :
- 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);
- }
- }
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 :
- 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);
- }
- }
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 extends E> 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 :
- 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);
- }
- }
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 extends E> c)
This method Inserts all of the elements in the specified collection into the list, starting at the specified position.
Example :
- 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);
- }
- }
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 :
- 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);
- }
- }
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); } }