0% found this document useful (0 votes)
156 views

Vector Example Programs

Vector is a dynamic array that can grow or shrink in size and store an unlimited number of elements. It is part of the Java Collection framework and found in the java.util package. Vectors can add and retrieve elements and check properties like size, capacity, and whether specific elements exist.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Vector Example Programs

Vector is a dynamic array that can grow or shrink in size and store an unlimited number of elements. It is part of the Java Collection framework and found in the java.util package. Vectors can add and retrieve elements and check properties like size, capacity, and whether specific elements exist.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Vector

Vector is like the dynamic array which can grow or shrink its size. Unlike
array, we can store n-number of elements in it as there is no size limit. It
is a part of Java Collection framework since Java 1.2. It is found in
the java.util package.

Java Vector Example


1. import java.util.*;  
2. public class VectorExample {  
3.        public static void main(String args[]) {  
4.           //Create a vector  
5.           Vector<String> vec = new Vector<String>();  
6.           //Adding elements using add() method of List  
7.           vec.add("Tiger");  
8.           vec.add("Lion");  
9.           vec.add("Dog");  
10.           vec.add("Elephant");  
11.           //Adding elements using addElement() method of Vector  
12.           vec.addElement("Rat");  
13.           vec.addElement("Cat");  
14.           vec.addElement("Deer");  
15.             
16.           System.out.println("Elements are: "+vec);  
17.        }  
18. }  

Output:

Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]

Java Vector Example 2


1. import java.util.*;  
2. public class VectorExample1 {  
3.        public static void main(String args[]) {  
4.           //Create an empty vector with initial capacity 4  
5.           Vector<String> vec = new Vector<String>(4);  
6.           //Adding elements to a vector  
7.           vec.add("Tiger");  
8.           vec.add("Lion");  
9.           vec.add("Dog");  
10.           vec.add("Elephant");  
11.           //Check size and capacity  
12.           System.out.println("Size is: "+vec.size());  
13.           System.out.println("Default capacity is: "+vec.capacity());  
14.           //Display Vector elements  
15.           System.out.println("Vector element is: "+vec);  
16.           vec.addElement("Rat");  
17.           vec.addElement("Cat");  
18.           vec.addElement("Deer");  
19.           //Again check size and capacity after two insertions  
20.           System.out.println("Size after addition: "+vec.size());  
21.           System.out.println("Capacity after addition is: "+vec.capacity());  
22.           //Display Vector elements again  
23.           System.out.println("Elements are: "+vec);  
24.           //Checking if Tiger is present or not in this vector         
25.             if(vec.contains("Tiger"))  
26.             {  
27.         System.out.println("Tiger is present at the index " +vec.indexOf("Tig
er"));  
28.             }  
29.             else  
30.             {  
31.                System.out.println("Tiger is not present in the list.");  
32.             }  
33.             //Get the first element  
34.       System.out.println("The first animal of the vector is = "+vec.firstElem
ent());   
35.           //Get the last element  
36.        System.out.println("The last animal of the vector is = "+vec.lastElem
ent());   
37.        }  
38. }  

Output:

Size is: 4
Default capacity is: 4
Vector element is: [Tiger, Lion, Dog, Elephant]
Size after addition: 7
Capacity after addition is: 8
Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]
Tiger is present at the index 0
The first animal of the vector is = Tiger
The last animal of the vector is = Deer

You might also like