0% found this document useful (0 votes)
20 views3 pages

Practical 14

Uploaded by

Prashant Bhamare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Practical 14

Uploaded by

Prashant Bhamare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a program to insert different elements in the vector and display them

import java.util.*;
public class VectorExample {
public static void main(String args[]) {
//Create a vector
Vector vec = new Vector(5);
//Adding elements using add() method of List

vec.addElement("Rat");
vec.addElement(new Integer(10));
vec.addElement(new Float(4.5));
vec.addElement(new Float(6.5));
System.out.println("Elements are: "+vec);
System.out.println("current size: "+vec.size());
}
}

Write a program to use different methods of vector class


import java.util.*;
public class VectorExample1 {
public static void main(String args[]) {
Vector<String> vec = new Vector<String>(4);
vec.add("Tiger");
vec.add("Lion");
vec.add("Dog");
vec.add("Elephant");
System.out.println("Size is: "+vec.size());
System.out.println("Default capacity is: "+vec.capacity());
System.out.println("Vector element is: "+vec);
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after addition is: "+vec.capacity());
System.out.println("Elements are: "+vec);
//Checking if Tiger is present or not in this vector
if(vec.contains("Tiger"))
{
System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));
}
else
{
System.out.println("Tiger is not present in the list.");
}
//Get the first element
System.out.println("The first animal of the vector is = "+vec.firstElement());
//Get the last element
System.out.println("The last animal of the vector is = "+vec.lastElement());
} }

You might also like