Java Vector add(E e) Method Example

In this tutorial we will learn add() method of java.util.Vector class. This method inserts the specified element to the end of the Vector.

Signature

public boolean add(E e)

This method appends the specified element to the end of this list.e is the element which will be appended and will return true on successful insertion.

Example

The following example shows the usage of java.util.Vector.add(e) method.

import java.util.Vector;

public class AddMethodVectorExample 
{
	public static void main(String args[])
	{
		// Create Vector vector1 object
		Vector vector1 = new Vector();
		
		//adding elements to Vector using add()
		vector1.add("Element1");
		vector1.add("Element2");
		vector1.add("Element3");
		boolean flag = vector1.add("Element4");
		
		System.out.println("Is the insertion successful ??"+flag);
		
		//Printing the elements of the Vector
		System.out.println("**Elements of the Vector**");
		for(String val : vector1)
		{
			System.out.println(val);
		}
	}
}

Output

Is the insertion successful ??true
**Elements of the Vector**
Element1
Element2
Element3
Element4

Leave a Reply

Your email address will not be published. Required fields are marked *