Array and Vector based on the following parameters:

Basic:

Array in java is an object that contains data of homogeneous (similar) type.
example, int a[]={5,6,4,55}; //this array can only store int data type

Vector can store heterogeneous data types, if restriction is not put using generics concept. See the example below:

package string;
importjava.util.Iterator;
importjava.util.Vector;
public class splitDemo {
      public static void main(String[] args) {
            Vector v = new Vector();
            v.add(1);    //integer
            v.addElement(“xyz”);  //string
            v.add(‘s’);  //char
            Iterator itr = v.iterator();
            while(itr.hasNext()){
                  System.out.println(itr.next());
            }
      }
}

OUTPUT:
1
xyz
In above example, you can see we have stored int, String and char objects in vector. 
Note: To make vector store homogeneous data type do the following:

Vector<String> v= new Vector<String>();

This will only allow storage of String object in vector.

Family:

Array does not belongs to Collection family. It is a primitive data type.

Vector implements List interface which is a part of collection hierarchy. Thus vector is a part of collection family.

Size:

Array needs to be assigned with the fixed size during initialization thus making it static in nature.

Vector resizes itself dynamically as per the need. Thus it is dynamic in nature.

Synchronized:

Array is non-synchronized in java.

Vector is synchronized in java.

Traversal:

Array can be traversed using for loop or enhanced for loop. It cannot use iterator or enumeration interface like vector or arraylist.

Vector can be traversed using iterator or enumeration interface

Declare/Instantiate/Initialization:

Array
Integer array
int a[]={5,6,4,55};

String array
String a[]={“Ajay”,“vijay”,“Rajesh”,“Mukesh”};

Similarly you can do for other data types like double, float, char, etc

Vector
Vector<String> vec=new Vector<String>();  //create vector
vec.add(“Ajay”);      //add element to vector
vec.add(“vijay”);

Source link

Difference: Array vs Vector in java

Leave a Reply

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