What is the difference between a vector and an array?
deekasha gunwant
Ranch Hand
Joined: May 06, 2000
Posts: 396
posted
0
hello nadia, both array and vectors are use to store data . the difference lies here.... --------------------------------------------- Array -------------------------------------------- 1)the size of array need to be declared in advance. 2)once declared array can't grow in size. 3)array can store primitive data types.like int,char,... -------------------------------------------- Vector -------------------------------------------- 1) no need to declare the size of vector. u may give its size & u may not. 2) Vector can always grow in size if u start adding more element to it than ur declared size. 3) Vector can store only object references. storing primitive data types is not possible in case of vectors.
--------------------------------------------- regards deekasha [This message has been edited by deekasha gunwant (edited July 04, 2000).]
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
hi, u can store primitives only by using the Wrapper classes. also u can add an element in a given position and also in the end in a vector. Adding an element at a given position pushes an existing element below thus increasing the size. This is not possible in an array as the old values are replaced by the new one. same is the case for removing of elements. In a vector removing an element reduces the size of the vector pushing the older values up by one position. A vector is synchronized, an array is not the example shows this. import java.util.*; class demo { public static void main(String[] args) { Vector a=new Vector(); a.add("one"); a.add("two"); a.add(1,"three"); Enumeration z=a.elements(); while(z.hasMoreElements()) { System.out.println(z.nextElement()); } int x[]=new int[2]; x[0]=8; System.out.println(x[0]); x[0]=10; System.out.println(x[0]); System.out.println(x[1]);// contains 0 not 8
}} Regds. Rahul
[This message has been edited by rahul_mkar (edited July 10, 2000).]