Java Program to Implement Vector

«
»
This Java program is to Implement Vector. The elements of a vector are stored contiguously.Like all dynamic array implementations, vectors have low memory usage and good locality of reference and data cache utilization

Here is the source code of the Java program to implement vector. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Vector<T> 
  5. {
  6.     private int capacity;
  7.     private int size;
  8.     private ArrayList<T> vector;
  9.     private static final int INCREMENT_FACTOR = 5;
  10.  
  11.     public Vector(int size)
  12.     {
  13.         this.size = size;
  14.         this.capacity = size + INCREMENT_FACTOR;
  15.         vector = new ArrayList<T>();
  16.     }
  17.  
  18.     public void store(int index, T value)
  19.     {
  20.         try 
  21.         {
  22.             vector.set(index, value);
  23.         } catch (IndexOutOfBoundsException indexOutBounds)
  24.         {
  25.             if (index >= 0 && (index < size))
  26.             {
  27.                 vector.add(index, value);
  28.             }
  29.             if (index >= 0 && (index >= size && index < capacity))
  30.             {
  31.                 vector.add(index, value);
  32.                 size = index + 1;
  33.                 if (size == capacity)
  34.                     capacity = capacity + INCREMENT_FACTOR;
  35.             }
  36.             if (index >= capacity)
  37.             {
  38.                 throw new IndexOutOfBoundsException();
  39.             }
  40.         }
  41.     }
  42.  
  43.     public T get(int index)
  44.     {
  45.         try
  46.         {
  47.             return vector.get(index);
  48.         } catch (IndexOutOfBoundsException indexOutBounds)
  49.         {
  50.         }
  51.         return null;
  52.     }
  53.  
  54.     public int getSize()
  55.     {
  56.         return size;
  57.     }
  58.  
  59.     public int getCapacity()
  60.     {
  61.         return capacity;
  62.     }
  63.  
  64.     public static void main(String... arg)
  65.     {
  66.         int size;
  67.         int num;
  68.         int value;
  69.  
  70.         Scanner scanner = new Scanner(System.in);
  71.         System.out.println("Enter the initial size of the vector");
  72.         size = scanner.nextInt();
  73.  
  74.         Vector<Integer> vector = new Vector<>(size);
  75.         System.out.println("Enter the number of elements ");
  76.         num = scanner.nextInt();
  77.  
  78.         System.out.println("Enter the values");
  79.         for (int index = 0; index < num; index++)
  80.         {
  81.             value = scanner.nextInt();
  82.             vector.store(index, value);
  83.         }
  84.  
  85.         System.out.println("The Entered Values are");
  86.         for (int index = 0; index < vector.getSize(); index++)
  87.         {
  88.             System.out.print(vector.get(index) + "\t");
  89.         }
  90.  
  91.         System.out.println("\nTHE SIZE OF THE VECTOR IS  " + vector.getSize());
  92.         System.out.println("THE CAPACITY OF THE VECTOR IS  " + vector.getCapacity());
  93.         scanner.close();
  94.     }
  95. }


$javac Vector.java
$java Vector
 
Enter the initial size of the vector
5
Enter the number of elements 
5
Enter the values
10 9 8 7 6
The Entered Values are
10	9	8	7	6	
THE SIZE OF THE VECTOR IS  5
THE CAPACITY OF THE VECTOR IS  10

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.