Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Jun 10, 1999

Dynamic Array Allocation

Java does not offer arrays that can be extended at run time, for primitives or Objects, so it is common to use a Vector for a set of elements that needs to be able to grow. Once the set is complete, if your code needs repeated access to the elements, perhaps to sort them, it would be more efficient to be able to manipulate an array. Fortunately, it is easy to obtain an array from a Vector. Suppose you have placed numerous Strings in a Vector. To obtain the corresponding String array takes just three lines of code:
 
    int count = yourVector.size();
    String[] yourArray = new String[count];
    yourVector.copyInto(yourArray);
If the Vector is not referenced again, the garbage collector will be able to recover the memory it required.
Tom Yarker
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date