What are the Benefits of Using Vectors Function in Java?

What are the Benefits of Using Vectors Function in Java?
What are the Benefits of Using Vectors Function in Java?


What are the Benefits of Using Vectors in Java

Vector: Vector is a predefined class and is used to store the data. We can store data of any data type(float, int, character, string). It is defined inside the package java. util. It is similar to a traditional Java array, except that it can grow as necessary to accommodate new elements. Elements of a vector can be accessed using index numbers and it starts from zero. Vectors are Synchronized. Vector implements a dynamic array that means it can grow or shrink as required. It is mostly used when there is no idea about the number of elements in an array.

👇👇👇👇👇

Polynomials in C++ - Encapsulation, Data Abstraction and Inheritance

There is three-way to create a vector.

Vector v = new Vector();
It will create an empty Vector of capacity 10. The default initial capacity of the vector is 10.

Vector v = new Vector(5);
It will create a Vector of an initial capacity of 5.

Vector v = new Vector(5,10);
Here 5 is the initial capacity and 10 is the capacity increment. It means upon insertion of the 6th element the size would be 15 (5+10).

How To Use Vector in Java?

import java.util.Vector;

import java.util.Enumeration;

class Icoderweb

{

public static void main(String args[])

{

Enumeration courses;

Vector courseName = new Vector();

 courseName.add("C");

courseName.add("C++");

courseName.add("JAVA");

courseName.add("PYTHON");

courses = courseName.elements();

while (courses.hasMoreElements())

{

System.out.println(courses.nextElement());

}

}

}

*****OUTPUT*****

C

C++

JAVA

PYTHON


How To Use Vector Function in Java?

👉Super Keyword Static Keyword


1. add(): add function can be used to insert the element at the end of the vector.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

Vector data = new Vector();

data.add("JAVA");

data.add("PYTHON");

for(int i=0;i< data.size();i++)

{

System.out.println(data.get(i));

}

}

}

*****OUTPUT*****
JAVA

PYTHON


2.addElement(): add Element function can be also used to insert the element at the end of the vector.

import java.util.Vector;

class Icoderweb

{

 public static void main(String args[])

{

Vector data = new Vector();

data.addElement("JAVA"); c

data.addElement("PHP");

for(int i=0;i< data.size();i++)

{

System.out.println(courseName.get(i));

}

}

}

*****OUTPUT*****
JAVA

PYTHON


3.size(): size can return the size of the vector.


import java.util.Vector;

class Icoderweb

{

 public static void main(String args[])

{

Vector data= new Vector();

data.addElement("JAVA");

data.addElement("PYTHON");

System.out.println("Size of Vector:"+data.size());

}

}

*****OUTPUT*****
Size of Vector:2


4. get(): get function can be used to get the element at the specified index.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

Vector data= new Vector();

data.addElement("JAVA");

data.addElement("PYTHON");

System.out.println(courseName.get(1));

}

}

*****OUTPUT*****

PYTHON


5. elementAt(): ElementAt function can be also used to get the element at specified index.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

Vector data= new Vector();

Data.addElement("JAVA");

data.addElement("PHP");

System.out.println(courseName.elementAt(1));

}

}

*****OUTPUT*****

PYTHON


6. capacity(): The capacity function can return the current capacity of the vector.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

Vector v1 = new Vector();

System.out.println("Capacity:"+v1.capacity());

Vector v2 = new Vector(5);

System.out.println("Capacity:"+v2.capacity());

}

}

*****OUTPUT*****

Capacity:10

Capacity:5


7.firstElement(): First Elements function can be returns the first element of vector.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

Vector v = new Vector();

v.addElement("JAVA");

v.addElement("C++");

v.addElement("PyTHON");

v.addElement("C");

System.out.println(v.firstElement());

}

}

*****OUTPUT*****
JAVA


8.lastElement(): Last Element function can be returns the last element of vector.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

Vector v = new Vector();

v.addElement("JAVA");

v.addElement("C++");

v.addElement("PyTHON");

v.addElement("C Language");

System.out.println(v.lastElement());

}

}

*****OUTPUT*****
C Language


9. contains(): The Contains function can check whether the specified element is present in the Vector. It returns boolean values true and false. If the given element presents in the vector return true otherwise false.

import java.util.Vector;

class Icpderweb

{

public static void main(String args[])

{

Vector v= new Vector();

v.addElement("JAVA");

v.addElement("C++");

v.addElement("PYTHON");

v.addElement("C Language");

System.out.println(v.contains("JAVA"));

System.out.println(v.contains("HTML"));

}

}

*****OUTPUT*****
True

False


10.isEmpty(): is Empty function can be returns true if Vector doesn’t have any element.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

Vector v= new Vector();

System.out.println(v.isEmpty());

v.addElement("JAVA");

v.addElement("PHP");

System.out.println(obj.isEmpty());

}

}

*****OUTPUT*****
True

False

11.remove(): Remove function can be used remove the element at specified index from vector.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

 Vector v = new Vector();

v.addElement("JAVA");

v.addElement("PHP");

v.addElement("PYTHON");

v.addElement("ANDROID");

System.out.println("Elements of vector");

System.out.println(v);

v.remove(2);

 System.out.println("Elements of vector after removing");

System.out.println(v);

}

}

 *****OUTPUT*****

Elements of vector [JAVA, PHP, PYTHON, ANDROID]

Elements of the vector after removing [JAVA, PHP, ANDROID]


12.removeElement(): Remove Element function removes the specifed element from vector.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

 Vector v = new Vector();

v.addElement("JAVA");

v.addElement("PHP");

v.addElement("PYTHON");

v.addElement("ANDROID");

System.out.println("Elements of vector");

System.out.println(v);

v.remove("ANDROID");

System.out.println("Elements of vector after removing");

System.out.println(obj);

}

}

*****OUTPUT*****

Elements of vector [JAVA, PHP, PYTHON, ANDROID]

Elements of the vector after removing [JAVA, PHP, PYTHON]


13.insertElementAt(): insert Elements can be used to insert the element at specified index.

import java.util.Vector;

class icoderweb

{

public static void main(String args[])

{

Vector v = new Vector();

v.addElement("JAVA");

v.addElement("PHP");

v.addElement("PYTHON");

v.addElement("ANDROID");

System.out.println("Elements of vector");

System.out.println(v);

v.insertElementAt("HTML",2);

System.out.println("Elements of vector after insertion");

System.out.println(v);

}

}

*****OUTPUT*****

Elements of vector [JAVA, PHP, PYTHON, ANDROID]

Elements of vector after insertion [JAVA, PHP, HTML, PYTHON, ANDROID]


14.set(): set function can be used to update the element at specified index.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

Vector v= new Vector();

v.addElement("JAVA");

v.addElement("PHP");

v.addElement("PYTHON");

v.addElement("ANDROID");

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

v.set(2,"HTML");

System.out.println("Vector elements after updation : "+v);

}

}

*****OUTPUT*****

Vector elements : [JAVA, PHP, PYTHON, ANDROID]

Vector elements after updation : [JAVA, PHP, HTML, ANDROID]


15.setElementAt():
set element function is also used to update the element at specified index.

import java.util.Vector;

class icoderweb

{

public static void main(String args[])

{

Vector v = new Vector();

v.addElement("JAVA");

v.addElement("PHP");

v.addElement("PYTHON");

v.addElement("ANDROID");

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

v.setElementAt("HTML",2);

System.out.println("Vector elements after updation : "+v);

}

}

*****OUTPUT*****

Vector elements : [JAVA, PHP, PYTHON, ANDROID]

Vector elements after updation : [JAVA, PHP, HTML, ANDROID]


16.removeAllElements():
Remove All Elements function can be used to remove all the elements from the vector.

import java.util.Vector;

class Icoderweb

{

public static void main(String args[])

{

 Vector v = new Vector();

v.addElement("JAVA");

v.addElement("PHP");

v.addElement("PyTHON");

v.addElement("ANDROID");

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

v.removeAllElements();

System.out.println("Vector elements after deletion : "+v);

}

}

*****OUTPUT*****

Vector elements : [JAVA, PHP, PYTHON, ANDROID]

Vector elements after deletion : []

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.