Vectors
Vectors
class can be used to create a generic dynamic array known as vector that can
hold objects of any type and any number. Vector class contained in the java.util.package.
The objects do not have to be homogenous. Vectors are created like arrays as
follows:
Vector vect=new
vector(); //declaring without size
Vector vect=new
vect(3); //declaring
with size.
Vectors
possess a number of advantages over arrays:-
- It is convenient
to use vectors to store objects
- A vector can be
used to store a list of objects that may vary in size.
- We can add and
delete objects from the list as and when required.
Vectors
can hold only Objects and not primitive types (eg,
5
).Common Vector Methods
There are many useful methods in
the Vector class and its parent classes. Here are some of the most useful.
v
is a Vector, i
is an int index, o
is an Object.
Method
|
Description
|
v.add(o) |
adds Object o to Vector v
|
v.add(i, o) |
Inserts Object o at index i,
shifting elements up as necessary.
|
v.clear() |
removes all elements from
Vector v
|
v.contains(o) |
Returns true if Vector v
contains Object o
|
v.firstElement(i) |
Returns the first element.
|
v.get(i) |
Returns the object at int index
i.
|
v.lastElement(i) |
Returns the last element.
|
v.listIterator() |
Returns a ListIterator that can
be used to go over the Vector. This is a useful alternative to the for loop.
|
v.remove(i) |
Removes the element at position
i, and shifts all following elements down.
|
v.set(i,o) |
Sets the element at index i to
o.
|
v.size() |
Returns the number of elements
in Vector v.
|
v.toArray(Object[]) |
The array parameter can be any
Object subclass (eg, String). This returns the vector values in that array
(or a larger array if necessary). This is useful when you need the generality
of a Vector for input, but need the speed of arrays when processing the data.
|
v.addElement(item) |
Adds the item specified to the
list at the end
|
v.elementAt(10) |
Gives the name of the 10th
object
|
v.size() |
Gives the number of objects
present
|
v.removeElement(item) |
Removes the specified item from
the list
|
v.removeElementAt(n) |
Removes the item stored in the
nth position of the list
|
v.removeAllElements(
) |
Removes all the elements in the
list
|
v.copyInto(array) |
Copies all items from list to
array
|
v.insertElementAt(item,n) |
Inserts the item at nth
position
|
Example Program:
import java.io.*;
import java.util.*;
class VectorDemo1
{
public static void main(String args[]) throws IOException
{
Vector v = new Vector( );
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
while(true)
{
System.out.println("1.Creation");
System.out.println("2.Insertion");
System.out.println("3.Deletion");
System.out.println("4.Display");
System.out.println("5.exit");
System.out.println("Enter Ur Choice:");
int n=Integer.parseInt(br.readLine());
switch(n)
{
case 1:
System.out.println("Enter
the Element:");
v.addElement(new
Integer(br.readLine()));
break;
case 2:
System.out.println("Enter the position to be inserted:");
int
x=Integer.parseInt(br.readLine());
v.insertElementAt(new
Integer(br.readLine()),x);
break;
case
3:System.out.println("Enter the position to be deleted:");
int
y=Integer.parseInt(br.readLine());
v.removeElementAt(y);
break;
case 4:
System.out.println("Elements are:");
for(int
i=0;i<v.size();i++)
System.out.println(v.get(i)+"
");
break;
case
5:System.exit(0);
}
}
}
}
Wrapper Classes
Vectors
cannot handle primitive data types like int,float,long,char and double.
Primitive data types may be converted into object types by using the wrapper
classes contained in Java.lang package.
Following table lists the primitive types and the corresponding wrapper classes:
Primitive
|
Wrapper
|
boolean
|
|
byte
|
java.lang.Byte
|
char
|
|
double
|
java.lang.Double
|
float
|
|
int
|
java.lang.Integer
|
long
|
java.lang.Long
|
short
|
java.lang.Short
|
No comments:
Post a Comment