Vector class extends AbstractList and implements List, RandomAccess, Cloneable, Serializable. Vector is synchronized.
Here are the Vector constructors:
Vector( )
Vector(int size)
Vector(int size, int incr)
Vector(Collection c)
The first form creates a default vector, which has an initial size of 10.
The second form creates a vector whose initial capacity is specified by size.
The third form creates a vector whose initial capacity is specified by size and whose increment is specified by incr.
The increment specifies the number of elements to allocate each time that a vector is resized upward.
The fourth form creates a vector that contains the elements of collection c.
Methods of a Vector class.
add(Object o): It adds the element in the end of the Vector
ex: vector.add(str);
size(): It gives the number of element in the vector.
ex: int size=vector.size();
elementAt(int index): It returns the element at the specified index.
ex: Object o=vector.elementAt(2);
firstElement(): It returns the first element of the vector.
ex: Object o=vector.firstElement();
lastElement(): It returns last element.
ex: Object o=vector.lastElement();
removeElementAt(int index): It deletes the element from the given index.
ex: vector.removeElementAt(2);
elements(): It returns an enumeration of the element
ex: Enumeration e=vector.elements();
import java.util.*;
public class VectorExample
{
public static void main(String[] args)
{
Vector vector = new Vector();
int primitiveType = 10;
Integer wrapperType = new Integer(20);
String str = "Sathya";
vector.add(primitiveType);
vector.add(wrapperType);
vector.add(str);
vector.add(2, new Integer(30));
System.out.println("the elements of vector: " + vector);
System.out.println("The size of vector are: " + vector.size());
System.out.println("The elements at position 2 is: "+vector.elementAt(2));
System.out.println("The first element of vector is: "+vector.firstElement());
System.out.println("The last element of vector is: "+vector.lastElement());
vector.removeElementAt(2);
Enumeration e=vector.elements();
System.out.println("The elements of vector: "+vector);
while(e.hasMoreElements())
{
System.out.println("The elements are: "+e.nextElement());
}
Iterator vItr = vector.iterator();
System.out.println("Elements in vector:");
while(vItr.hasNext())
System.out.print(vItr.next() + " ");
}
}
Output:
the elements of vector: [10, 20, 30, Sathya]
The size of vector are: 4
The elements at position 2 is: 30
The first element of vector is: 10
The last element of vector is: Sathya
The elements of vector: [10, 20, Sathya]
The elements are: 10
The elements are: 20
The elements are: Sathya
Elements in vector:
10 20 Sathya
Here are the Vector constructors:
Vector( )
Vector(int size)
Vector(int size, int incr)
Vector(Collection c)
The first form creates a default vector, which has an initial size of 10.
The second form creates a vector whose initial capacity is specified by size.
The third form creates a vector whose initial capacity is specified by size and whose increment is specified by incr.
The increment specifies the number of elements to allocate each time that a vector is resized upward.
The fourth form creates a vector that contains the elements of collection c.
Methods of a Vector class.
add(Object o): It adds the element in the end of the Vector
ex: vector.add(str);
size(): It gives the number of element in the vector.
ex: int size=vector.size();
elementAt(int index): It returns the element at the specified index.
ex: Object o=vector.elementAt(2);
firstElement(): It returns the first element of the vector.
ex: Object o=vector.firstElement();
lastElement(): It returns last element.
ex: Object o=vector.lastElement();
removeElementAt(int index): It deletes the element from the given index.
ex: vector.removeElementAt(2);
elements(): It returns an enumeration of the element
ex: Enumeration e=vector.elements();
import java.util.*;
public class VectorExample
{
public static void main(String[] args)
{
Vector vector = new Vector();
int primitiveType = 10;
Integer wrapperType = new Integer(20);
String str = "Sathya";
vector.add(primitiveType);
vector.add(wrapperType);
vector.add(str);
vector.add(2, new Integer(30));
System.out.println("the elements of vector: " + vector);
System.out.println("The size of vector are: " + vector.size());
System.out.println("The elements at position 2 is: "+vector.elementAt(2));
System.out.println("The first element of vector is: "+vector.firstElement());
System.out.println("The last element of vector is: "+vector.lastElement());
vector.removeElementAt(2);
Enumeration e=vector.elements();
System.out.println("The elements of vector: "+vector);
while(e.hasMoreElements())
{
System.out.println("The elements are: "+e.nextElement());
}
Iterator vItr = vector.iterator();
System.out.println("Elements in vector:");
while(vItr.hasNext())
System.out.print(vItr.next() + " ");
}
}
Output:
the elements of vector: [10, 20, 30, Sathya]
The size of vector are: 4
The elements at position 2 is: 30
The first element of vector is: 10
The last element of vector is: Sathya
The elements of vector: [10, 20, Sathya]
The elements are: 10
The elements are: 20
The elements are: Sathya
Elements in vector:
10 20 Sathya
0 comments:
Post a Comment