0% found this document useful (0 votes)
7 views7 pages

Vector

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Vector

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Vector:

The Underlying Data Structure is Resizable Array OR Growable Array.


 Insertion Order is Preserved.
 Duplicate Objects are allowed.
 Heterogeneous Objects are allowed.
 null Insertion is Possible.
 Implements Serializable, Cloneable and RandomAccess interfaces.

 Every Method Present Inside Vector is Synchronized and Hence Vector Object is
Thread Safe.

 Vector is the Best Choice if Our Frequent Operation is Retrieval.


 Worst Choice if Our Frequent Operation is Insertion OR Deletion in the Middle.

Constructors:
1) Vector v = new Vector();

 Creates an Empty Vector Object with Default Initial Capacity 10.


 Once Vector Reaches its Max Capacity then a New Vector Object will be Created
with

new Capacity = current capacity *2;

Example :

package list;

import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector();


System.out.println(v.capacity());
v.add(67);
v.add(89);
v.add(67);
v.add("Santosh");
v.add(null);
v.add(67);
v.add(89);
v.add(67);
v.add("Santosh");
v.add(null);
v.add(89);
System.out.println(v.capacity());
System.out.println(v);

}
2)Vector v=new Vector(initial capacity);

package list;

import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector(12);


System.out.println(v.capacity());
v.add(67);
v.add(89);
v.add(67);
v.add("Santosh");
v.add(null);
v.add(67);
v.add(89);
v.add(67);
v.add("Santosh");
v.add(null);
v.add(89);
v.add(null);
v.add(89);
System.out.println(v.capacity());
System.out.println(v);

3) Vector v = new Vector(initial Capacity, int incremental Capacity);

package list;

import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector(12,6);


System.out.println(v.capacity());
v.add(67);
v.add(89);
v.add(67);
v.add("Santosh");
v.add(null);
v.add(67);
v.add(89);
v.add(67);
v.add("Santosh");
v.add(null);
v.add(89);
v.add(null);
v.add(89);
System.out.println(v.capacity());
System.out.println(v);

4) Vector v = new Vector(Collection c);

Example :

package list;

import java.util.ArrayList;
import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

ArrayList al=new ArrayList();


al.add(56);
al.add(78);
al.add(89);
System.out.println(al);

Vector v = new Vector(al);


System.out.println(v.capacity());
v.add(67);
v.add(89);
v.add(67);
v.add("Santosh");
v.add(null);
v.add(67);
v.add(89);
v.add(67);
v.add("Santosh");
v.add(null);
v.add(89);
v.add(null);
v.add(89);
System.out.println(v.capacity());
System.out.println(v);

}
Methods of Vector Class:
1) To Add Elements:
 add(Object o)Collection
 add(int index, Object o)List
 addElement(Object o) Vector

Example :

package list;

import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector(12);

v.add(67);
v.addElement(89);
v.add(67);
v.add("Santosh");
v.add(null);
System.out.println(v);
v.add(1, "Bikkad");
System.out.println(v);

2) To Remove Elements:
 remove(Object o) Collection
 removeElement(Object o)Vector
 remove(int index) List
 removeElementAt(int index)Vector
 clear() Collection
 removeAllElements()Vector

Example 1:
package list;

import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector(12);

v.add(67);
v.addElement(89);
v.add(68);
v.add("Santosh");
v.add(null);
System.out.println(v);
v.remove(1);
v.removeElementAt(2);
System.out.println(v);

Example2 :

package list;

import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector(12);

v.add(67);
v.addElement(89);
v.add(68);
v.add("Santosh");
v.add(null);
System.out.println(v);
Integer valueOf = Integer.valueOf(89);
v.remove(valueOf);
Integer valueOf2 = Integer.valueOf(68);
v.removeElement(valueOf2);
System.out.println(v);

Example 3:

package list;

import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector(12);

v.add(67);
v.addElement(89);
v.add(68);
v.add("Santosh");
v.add(null);
System.out.println(v);
// v.clear();
v.removeAllElements();
System.out.println(v);

3) To Retrive Elements:
 Object get(int index)List
 Object elementAt(int index)Vector
 Object firstElement() Vector
 Object lastElement()Vector

Example :

package list;

import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector(12);

v.add(67);
v.addElement(89);
v.add(68);
v.add("Santosh");
v.add(null);
System.out.println(v);
System.out.println(v.get(1));
System.out.println(v.elementAt(2));
System.out.println(v.firstElement());
System.out.println(v.lastElement());
System.out.println(v);

4) Some Other Methods:


 int size()
 int capacity()
 Enumeration element()

Example :

package list;
import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector(12);

v.add(67);
v.addElement(89);
v.add(68);
v.add("Santosh");
v.add(null);
System.out.println(v.size());
System.out.println(v.capacity());
System.out.println(v);

You might also like