Vector Class In Java

  • Vector cast inwards coffee implements List interface of collection framework.
  • Vector cast is synchronized.
  • If you lot don't know size of array too hence you lot tin purpose vector cast every bit size of vector tin grow too shrink every bit per adding too removing items.
  • As vector cast is synchronized, It volition hand misfortunate performance on add, delete, update too search operations.
  • Elements of vector tin live accessed using it's integer index.
vector cast hierarchy inwards java

Important method of vector class
  • void addElement(Object element) : It volition add together specified chemical subdivision at halt of vector.
  • int capacity() : It volition furnish the electrical flow capacity of vector.
  • int size() : This method volition furnish electrical flow size of vector.
  • void setSize(int size) : It volition railroad train size of vector using given size value.
  • boolean contains(Object element) : It volition furnish truthful if specified chemical subdivision introduce inwards vector. Else it volition furnish false.
  • boolean containsAll(Collection c) : It volition furnish truthful if vector contains all values of given collection c.
  • Object elementAt(int index) : It volition furnish chemical subdivision which is located at specified index of vector.
  • Object firstElement() : It volition furnish commencement chemical subdivision of vector.
  • Object lastElement() : It volition furnish final chemical subdivision of vector.
  • Object get(int index) : It volition furnish chemical subdivision located at the specified index of vector.
  • boolean isEmpty() : It volition furnish truthful if vector is empty.
  • boolean removeElement(Object element) : It volition take away given chemical subdivision from vector.
  • boolean removeAll(Collection c) : It volition take away all elements of collection c from vector.
  • void setElementAt(Object element, int index) : It volition railroad train specified chemical subdivision at given index of vector.
Bellow given sample plan volition present you lot how to purpose vector cast too it's dissimilar methods.

Vector cast example
package JAVAExamples;  import java.util.Enumeration; import java.util.Vector;  populace cast VectorExample {  populace static void main(String args[]) {   //Initial vector capacity is 2. Increment it yesteryear 2 when required.   Vector 5 = novel Vector(2,2);   System.out.println("Initial capacity of vector : "+v.capacity());   v.addElement("one");   v.addElement("two");   v.addElement("three");     System.out.println("Capacity of vector afterward adding 3 elements inwards vector : "+v.capacity());    //Get size of vector.   System.out.println("Size of vector : "+v.size());    //Get commencement chemical subdivision of vector.   System.out.println("First chemical subdivision of vector : "+v.firstElement());      //Get final chemical subdivision of vector.   System.out.println("Last chemical subdivision of vector : "+v.lastElement());      //Add novel chemical subdivision inwards vector.   v.add(2, "New Element");      //Print all elements of vector using Enumeration.   Enumeration vEnum = v.elements();   System.out.print("Current vector elements : ");   while(vEnum.hasMoreElements()){    System.out.print(vEnum.nextElement() + ", ");   }      System.out.println();      //Check if vector is empty.   System.out.println("Vector is empty? : "+v.isEmpty());    } }

Output :
Initial capacity of vector : 2 Capacity of vector afterward adding 3 elements inwards vector : iv Size of vector : 3 First chemical subdivision of vector : ane Last chemical subdivision of vector : iii Current vector elements : one, two, New Element, three,  Vector is empty? : false

Above instance shows you lot usage of generally used methods of vector class.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post