Hi, I have just started the Java. Please let me know what this statement says
class ABC{
transient Vector<int> temp[];
ABC(int max)
{
this.temp = new Vector [max];
}
Is it creating a vector of int which size is max?
I am C++ person.
Hi, I have just started the Java. Please let me know what this statement says
class ABC{
transient Vector<int> temp[];
ABC(int max)
{
this.temp = new Vector [max];
}
Is it creating a vector of int which size is max?
I am C++ person.
That creates an array of Vector objects. The length of the array is whatever is passed in as "max".
If you want a single Vector, leave off the []'s. A couple of changes are necessary to get the above code to compile.
import java.util.Vector;
class ABC
{
transient Vector temp[];
ABC(int max)
{
this.temp = new Vector[max];
}
}
No,
To create a Vector of initial capacity max you should
Vector<Integer> v = new Vector (max)
Note two things:
Usage of Integer and not int. In Java, Integer is an object, while int is a primitive type. Collections can't use primitive types, they use objects.
The capacity of the v is not limited to max
elements. It will grow if you insert more than max
Integers.
But let the API page do the talking
The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
Each vector tries to optimize storage management by maintaining a
capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.
Here is the javadoc for the Vector constructor in question. What you are most likely looking for is
this.temp = new Vector<int>(max);
class ABC{ transient Vector<Integer> temp[];
ABC(int max) { this.temp = new Vector [max]; }
read Integer instead of int. Yes it's working code.