Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
Dec 23, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Arrays are Cheaper and Safer than Vectors

In Java, you can use either a Vector or a one-dimensional array to store an ordered collection of objects. The difference between the two types of storage is that the array allocates storage for a fixed number of elements where as the Vector can grow dynamically. Another difference is that an array can be used to store primitives (int, boolean, etc.) where as a Vector can only store pure objects. You should also keep in mind that arrays are type-safe. An array can only store objects of a particular class and only objects of that class can be retrieved from it. Assigning that object to a variable of its type requires no explicit casting. For example:
SomeObject[] soArray = new SomeObject[10];

// Fill array with some object
for (int i = 0; i < 10; i++)
     soArray[i] = new SomeObject(i);

// Get objects from the array
for (int i = 0; i < 10; i++)
    SomeObject so = soArray[i];
The variable soArray only holds variables of the type
Ajit Sagar
Comment and Contribute






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



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