• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Difference between vector and array

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between a vector and an array?
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic