Vector add() method in Java

In this post, we will be looking at the add methods of the Vector class in Java. There are two overloaded implementations of the add method which are listed below.

Now, let’s have a look at above methods one by one.

public synchronized boolean add(E e)

  • Method declaration – public synchronized boolean add(E e)
  • What does it do? – It will append the element passed in the argument at the end of the vector
  • What does it return? – It will return true if the element is successfully added at the end of the vector

We will take a vector of strings and will try to add the element at the end of the vector.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();
		v.add("first"); // this will add the element at the end of the string
		v.add("second"); // this will again add the element at the end of the string

		System.out.println("Vector: " + v.toString()); // to print the elements of the vector
	}
}

Output –

Vector: [first, second]
What if we try to use the add method on a null vector?

This will throw a NullPointerException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();
		v = null;
		v.add("first"); // this will add the element at the end of the string
		v.add("second"); // this will again add the element at the end of the string

		System.out.println("Vector: " + v.toString()); // to print the elements of the vector
	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Vector.add(Object)" because "v" is null
Now, what if we tried to add a null object into a vector?

In this case, that null object will be added into the vector successfully as it will be treated as a normal object being added into the vector. ( though that object itself is null ).

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();
		v.add(null); // this will add the element at the end of the string
		v.add("second"); // this will again add the element at the end of the string

		System.out.println("Vector: " + v.toString()); // to print the elements of the vector
	}
}

Output –

Vector: [null, second]

public void add(int index, E element)

  • Method declaration – public void add(int index, E element)
  • What does it do? It inserts the element passed in the arguments at the specified index ( also passed in the arguments ). Remember the indexing here is 0-index, which means, if we want to add the element at 4th position, then we need to pass 3 in the arguments as the index
  • What does it return? It does return anything as the return type of the function is void
public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();
		v.add("first"); // add the element at the end
		v.add("third"); // add the element at the end

		System.out.println("Vector: " + v.toString());

		// adding the element at the second position
		// by passing "1" as an index in the arguments
		v.add(1, "second");

		System.out.println("Vector now: " + v.toString());

	}
}

Output –

Vector: [first, third]
Vector now: [first, second, third]
What if we passed a negative index or index > size ?

It will throw ArrayIndexOutOfBoundsException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();
		v.add("first"); // add the element at the end
		v.add("third"); // add the element at the end

		System.out.println("Vector: " + v.toString());

		// ******passing negative index******
		v.add(-1, "second");

		System.out.println("Vector now: " + v.toString());

	}
}

Output –

Vector: [first, third]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: source index -1 out of bounds for object array[10]
	at java.base/java.lang.System.arraycopy(Native Method)
	at java.base/java.util.Vector.insertElementAt(Vector.java:597)
	at java.base/java.util.Vector.add(Vector.java:827)

Please visit this link if you want to know more about the Vector class in java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.