wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Difference between vector and array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between vector and array" Watch "Difference between vector and array" New topic
Author

Difference between vector and array

Nadia Waring
Greenhorn

Joined: Jul 04, 2000
Posts: 1
What is the difference between a vector and an array?
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
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
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between vector and array
 
Similar Threads
What is difference between ArrayList and vector?
Vector vs. Array
i cant understand this code
Question in Collection?
what is the difference between arrays,vector and Array List?