• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Difference between vector and array  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!