Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

difference between array and vector

4 views
Skip to first unread message

dragon ball

unread,
Jun 9, 2002, 8:38:28 AM6/9/02
to
Hi all,

When should I choose array and when should I choose vector ? I know that
array is for static number of element while vector is for dynamic. But why
should I choose array for static ? is it because of it will have better
performace ? if yes, by how much ?

Perseus


Richard Reynolds

unread,
Jun 9, 2002, 8:38:03 AM6/9/02
to

Arrays are faster by enough that you should choose them over Vectors if
possible.
You should also use the ArrayList class rather than Vector for dynamic
arrays, it has the same functionality as Vector but is quicker. Vectors have
a synchronised implementation for thread safety so you can use them if you
need this. However, you can provide your own thread safety precautions and
still use ArrayList i.e. you may only need to synchronise on a particular
object, not the whole ArrayList etc.


Gerard Airey

unread,
Jun 9, 2002, 9:40:53 AM6/9/02
to

One advantage of arrays over Vectors is that you can use an array of the
actual type you are storing. This means that when extracting elements you do
not need to cast to the correct type, unlike Vectors which store everything
as an Object.

eg

an array of Strings

String[] StringArray=new String[] {"1st", "2nd"};
String SingleString=StringArray[1];

a Vector of Strings

Vector StringVector=new Vector(2);
StringVector.addElement(new String("1st"));
StringVector.addElement(new String("2nd"));
String SingleString=(String)StringVector.elementAt(1);


Gerard Airey.

Ian Brandt

unread,
Jun 9, 2002, 11:31:31 AM6/9/02
to

"dragon ball" <perseus...@hotmail.com> wrote in message
news:3d034a9e$1...@newsgate.hknet.com...

If you're not familiar, the source for (at least a lot of) the
standard Java library is available. Usually it comes with the JDK
([JAVA_HOME]/src.zip), or otherwise you can dig around for it on the
website. Always a worth while read (and usually it sets a good
example). I'd read through java/util/Vector.java. It's fairly
straight forward, and well documented. It is based on an Object[], so
you can see what it adds as far as overhead. Also check out
ArrayList.java and Collections.java (especially
Collections.synchronizedCollection(Collection c) and the like).
ArrayList is like a Vector with out the synchronization, but you can
always "synchronize it" using Collections.synchronizedList.

As for performance numbers for Vector vs. ArrayList vs.
Collections.synchronizedList(ArrayList) vs. native array,,, no idea.
The real overhead of the collections is the synchronization, and the
enusreCapacity call on each add(Object) like call. The ensureCapacity
method relies on System.arraycopy, which is a native method. It's
performance is likely going to vary based on VM/platform, as is
synchronization performance. If you're writing something that is
super performance critical, there's really not going to be any way
around doing some experimentation on the particular platform(s) you're
writing towards.

The way I look at it is this, if you need just need a basic storage
bin of static size, and thread safety is not an issue (or you've got
it covered already), use an array. If you require dynamic size,
synchronization, or would benefit from the other aspects of the
java.util collections (e.g. SortedSet, LinkedList), use them. As for
the type safety issue, check out:
http://www.jcp.org/jsr/detail/14.jsp. Type-safe collections are coming
(soon?!).

Good Luck,

Ian

Marshall Spight

unread,
Jun 9, 2002, 2:09:58 PM6/9/02
to
"dragon ball" <perseus...@hotmail.com> wrote in message news:3d034a9e$1...@newsgate.hknet.com...
>

You should use arrays when they best suit the abstraction you are trying to
capture. This is primarily an issue of when the size of the list is fixed vs.
variable.

There's also the type-safety argument for arrays.


Marshall

0 new messages