Sign in to follow this  

[java] question about vectors

This topic is 4667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know that vectors have the ability to expand their size. Am I supposed to call a special add command to increase the size of a vector? In other words, can I tell the vector to insert some object at a specific location, and if the vector is not already that big, it will automatically stretch? hope that makes sense, thanks everyone

Share this post


Link to post
Share on other sites
All you need to know.

You can insert anywhere within the vector, from 'at the beginning' to 'at the end'. Items will get pushed back to make room for the insertion (e.g. if you insert at the beginning, then everything is shifted; if you insert at the end, then it just gets tacked on to the end and nothing else moves). The vector will resize itself as needed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the vector's add method will add an element to the end of the vector

example:

Vector myVector = new Vector();
myVector.add( new Integer(1) );

// the vector has one element

myVector.add( new Integer(2) );

// the vector now has two elements;

Share this post


Link to post
Share on other sites
Incidentally, Vector is depreciated - you should be using ArrayList instead (functionally the same, but unsyncronised). If you need a thread-safe ArrayList then you can wrap it in a thread-safe interface, which is described in the ArrayList docs.

Share this post


Link to post
Share on other sites

This topic is 4667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this