Hello friends how are you, today in this blog i will teach you what is Vector function, types of Vector function, how Vector functions work and many programs using Vector functions in a very simple way.
Let's start
Here we are going to learn all Vector functions one by one
- It is used to insert the element at the end of vector.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector courseName = new Vector();
/*adding data to vector*/
courseName.add("JAVA");
courseName.add("PHP");
/*Accessing data of vector*/
for(int i=0;i< courseName.size();i++)
{
/*printing data of vector*/
System.out.println(courseName.get(i));
}
}
}
/*
Output
JAVA
PHP
*/
- 2.addElement()
- It is also used to insert the element at the end of vector.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector courseName = new Vector();
/*adding data to vector*/
courseName.addElement("JAVA");
courseName.addElement("PHP");
/*Accessing data of vector*/
for(int i=0;i< courseName.size();i++)
{
/*printing data of vector*/
System.out.println(courseName.get(i));
}
}
}
/*
Output
JAVA
PHP
*/
- 3.size()
- It returns the size of the vector.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector courseName = new Vector();
/*adding data to vector*/
courseName.addElement("JAVA");
courseName.addElement("PHP");
System.out.println("Size of vector="+courseName.size());
}
}
/*
Output
Size of vector=2
*/
- 4.get()
- It is used to get the element at specified index.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector courseName = new Vector();
/*adding data to vector*/
courseName.addElement("JAVA");
courseName.addElement("PHP");
/*Accessing element of
vector using index value*/
System.out.println(courseName.get(1));
}
}
/*
Output
PHP
*/
- 5.elementAt()
- It is also used to get the element at specified index.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector courseName = new Vector();
/*adding data to vector*/
courseName.addElement("JAVA");
courseName.addElement("PHP");
/*Accessing element of
vector using index value*/
System.out.println(courseName.elementAt(1));
}
}
/*
Output
PHP
*/
- 6.capacity()
- It returns the current capacity of vector.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj1 = new Vector();
/*default capacity of vector is 10*/
System.out.println("Capacity="+obj1.capacity());
/*here 5 is the capacity*/
Vector obj2 = new Vector(5);
System.out.println("Capacity="+obj2.capacity());
}
}
/*
Output
Capacity=10
Capacity=5
*/
- 7.firstElement()
- It returns the first element of vector.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
obj.addElement("PyTHON");
obj.addElement("ANDROID");
/*Accessing first element of vector */
System.out.println(obj.firstElement());
}
}
/*
Output
JAVA
*/
- 8.lastElement()
- It returns the last element of vector.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
obj.addElement("PyTHON");
obj.addElement("ANDROID");
/*Accessing last element of vector */
System.out.println(obj.lastElement());
}
}
/*
Output
ANDROID
*/
- 9.contains()
- It is used to check whether the specified element is present in the Vector.
- It returns boolean value(true/false).
- If the given element presents in the vector returns true otherwise false.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
obj.addElement("PyTHON");
obj.addElement("ANDROID");
System.out.println(obj.contains("PHP"));
System.out.println(obj.contains("HTML"));
}
}
/*
Output
true
false
*/
- 10.isEmpty()
- It returns true if Vector doesn’t have any element.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*currently there is no element in vector*/
System.out.println(obj.isEmpty());
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
/*vector has two elements JAVA and PHP*/
System.out.println(obj.isEmpty());
}
}
/*
Output
true
false
*/
- 11.remove()
- It removes the element at specified index from vector.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
obj.addElement("PyTHON");
obj.addElement("ANDROID");
System.out.println("Elements of vector");
System.out.println(obj);
/*removing element from vector*/
obj.remove(2);/*removes element of index 2*/
System.out.println("Elements of vector after removing");
System.out.println(obj);
}
}
/*
Output
Elements of vector
[JAVA, PHP, PyTHON, ANDROID]
Elements of vector after removing
[JAVA, PHP, ANDROID]
*/
- 12.removeElement()
- It removes the specifed element from vector.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
obj.addElement("PyTHON");
obj.addElement("ANDROID");
System.out.println("Elements of vector");
System.out.println(obj);
/*removing element from vector*/
obj.remove("ANDROID");
System.out.println("Elements of vector after removing");
System.out.println(obj);
}
}
/*
Output
Elements of vector
[JAVA, PHP, PyTHON, ANDROID]
Elements of vector after removing
[JAVA, PHP, PyTHON]
*/
- 13.insertElementAt()
- It is used to insert the element at specified index.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
obj.addElement("PyTHON");
obj.addElement("ANDROID");
System.out.println("Elements of vector");
System.out.println(obj);
/*Insert element at index 2*/
obj.insertElementAt("HTML",2);
System.out.println("Elements of vector after insertion");
System.out.println(obj);
}
}
/*
Output
Elements of vector
[JAVA, PHP, PyTHON, ANDROID]
Elements of vector after insertion
[JAVA, PHP, HTML, PyTHON, ANDROID]
*/
- 14.set()
- It is used to update the element at specified index.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
obj.addElement("PyTHON");
obj.addElement("ANDROID");
System.out.println("Vector elements : "+obj);
/*setting HTML at index 2*/
obj.set(2,"HTML");
System.out.println("Vector elements after updation : "+obj);
}
}
/*
Output
Vector elements : [JAVA, PHP, PyTHON, ANDROID]
Vector elements after updation : [JAVA, PHP, HTML, ANDROID]
*/
- 15.setElementAt()
- It is also used to update the element at specified index.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
obj.addElement("PyTHON");
obj.addElement("ANDROID");
System.out.println("Vector elements : "+obj);
/*setting HTML at index 2*/
obj.setElementAt("HTML",2);
System.out.println("Vector elements after updation : "+obj);
}
}
/*
Output
Vector elements : [JAVA, PHP, PyTHON, ANDROID]
Vector elements after updation : [JAVA, PHP, HTML, ANDROID]
*/
- 16.removeAllElements()
- It is used to remove all the elements from the vector.
import java.util.Vector;
class Program
{
public static void main(String args[])
{
/*creating object of vector*/
Vector obj = new Vector();
/*adding data to vector*/
obj.addElement("JAVA");
obj.addElement("PHP");
obj.addElement("PyTHON");
obj.addElement("ANDROID");
System.out.println("Vector elements : "+obj);
/*removing all the elements of vector*/
obj.removeAllElements();
System.out.println("Vector elements after deletion : "+obj);
}
}
/*
Output
Vector elements : [JAVA, PHP, PyTHON, ANDROID]
Vector elements after deletion : []
*/
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other.
Thanks.😊
0 Comments