Vector Classes
1. Vector
class
is
present
under
the
package
'util' (java.util.vector).
2.Vector class is same as ArrayList but vector methods are synchronized
for
safety
purpose.
3. Vector is the only class
other than ArrayList which provides random access to the data elements.
4. Vector class is used to create the vector array which is dynamic in nature & stores homogeneous as well as heterogeneous type of data.
5. There is no upper limit to store number of data values into vector array i.e. vector array is growable.
6. Vector cannot store basic data type directly. It only requires the data in the form of object numbers.
7. Vector array can be declared without specifying
the
size. A vector array without size can store unknown no of data elements that may vary in size.
Difference between Array and Vector
Advantages of Vector
1. Vector provides a way to store
heterogeneous
and homogeneous
type
of
data
in the form of object.
2. Vector can be used to store the data elements that may vary in size.
3. Vector
class provides specialized methods for insertion, deletion, copy & other Manipulations.
Drawbacks of Vector
1. Vector
cannot
store
basic
data
type directly.
2. Vector can only store the object type of data.
3. To store the basic data type in a vector, we require the conversion
from
basic
data
types to object numbers using wrapper classes.
Constructor of Vector class
1. Default Constructor: - To create a vector array width default size = 10, we must execute the default Constructor.
Ex..
Vector
V
= new
Vector ();
2. Parameterized Constructor width size as n: In the following constructor, vector array created with initial size n and once the size of vector array is full, the size
will be incremented in multiples of n.
Ex..
Vector V = new Vector (n);
3. Parameterized Constructor (two parameter):
Ex. Vector V = new
Vector (n, m);
Where n = initial size of
vector
array, m
= increment of array as the capacity becomes full.
Non-static Methods of vector class: Create a vector array width default size.
Ex. Vector V = new Vector ();
1. v.addElement (data):
It adds the data element at the end of vector.
2. v.insertElementAt (data, n)::The above method inserts the data element at nth
position in a vector.
3. v.removeElementAt (n): :It remove data element from nth position of vector array
4. v1.copyInto
(v2):
It copies vector array v1 into
vector
array
v2
5. v.size():It returns the size of vector array.
6. v.removeElementAll(): The
method
makes
the
vector
array
empty.
7. v.removeElement
(data):
It removes
1st occurrence of data element
in
the
vector
array.
8. v.firstElement(): It
gives first element of vector array.
9.
v.lastElement(): It gives last element of vector array.
10. v.elementAt (n)
:
The above method returns data element from nth position.
11. v.contains(data):
It returns true if vector contains the data otherwise it returns false.
12. v1.equals(v2):
It returns
true
if
v1
is equal to v2 otherwise
false.
1. java.util.Random: Random class generates a stream of pseudo-random numbers. To create a new random number generator, use one of the following methods: new Random(), new Random(long seed).
The form
new Random () initializes
the generator to a value based on the current time. The form new Random(long seed) seeds the random number generator with a
specific
initial
value; use this if an application
requires a repeatable stream of pseudo-random
numbers. To create a pseudo- random number, use one of the following functions:
Random(): Creates a new random number generator.
Random(long): Creates a new random number generator using a single long seed.
nextDouble(): Generates
a pseudorandom uniformly distributed
double
value
between 0.0 and 1.0.
nextFloat(): Generates
a pseudorandom uniformly distributed
float
value
between
0.0 and 1.0.
nextInt(): Generates a pseudorandom
uniformally
distributed
int
value.
nextLong(): Generate
a
pseudorandom
uniformally
distributed
long
value.
2. The java.util.Date: A wrapper for a date. This class lets you manipulate dates in a
system independent way. To
print
today's
date use:
Date
d= new Date(); System.out.println("today = " + d);
To
find
out what day corresponds to a particular date:
Date d = new Date(63, 0, 16); // January 16, 1963
System.out.println("Day of
the
week: "
+
d.getDay());
getMinutes()
Returns the minute.
getMonth()
Returns the month.
getSeconds()
Returns the second.
getTime()
Returns milliseconds.
getYear()
Returns
the year after 1900.
setDate(int) :Sets the date
setDay(int) :Sets the day of the week.
setHours(int) :Sets the hours.
setMinutes(int) :Sets the minutes.
setMonth(int):Sets the month.
setSeconds(int) :Sets the seconds
Post a Comment