Array and Vector differs 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:
OUTPUT:
s
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:
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");
You may also like to read:
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;
import
java.util.Iterator;
import
java.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());
}
}
}
1
xyz
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");
You may also like to read: