initialize a vector with data

J

Jacques Chaurette

Sorry for this basic question but I cannot find anywhere how to initialed a vector with data.

Thanks.

Jacques
 
R

Roedy Green

Sorry for this basic question but I cannot find anywhere how to initialed a vector with data.

Whenever you need an introduction to something in Java, one place you
can usually get started is the Java glossary. It will give you an
overview and point you to more detailed information, and usually give
you a brief code sample of the basic operations.
http://mindprod.com/jgloss/jgloss.html

Just look up the word, in this case Vector.

http://mindprod.com/jgloss/vector.html

It will suggest you try ArrayList instead.
 
J

Jacques Chaurette

T

Thomas Weidenfeller

Jacques said:
Sorry for this basic question but I cannot find anywhere how to
initialed a vector with data.

In general, consider using ArrayList instead of Vector. It basically
does the same, but ArrayList is in general a faster.

Regarding the initialization of a Vector (or ArrayList):

Since Java 1.2:

Vector v = new Vector(Arrays.asList(<array-of-elements>));
or
Vector v = new Vector();
v.addAll(Arrays.asList(<array-of-elements>));

or a simple loop with calls to add().

In Java 1.5 you an also do (the stuff above also works):

Vector v = new Vector();
Collections.addAll(v, <array-or-varlist-of-elements>);

The 1.5 documentation claims this can be faster than Arrays.asList(). In
1.5. Arrays.asList() also allows a variable length list as argument.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top