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.

import java.util.ArrayList;
import java.util.Scanner;
 
public class Vector<T> 
{
    private int capacity;
    private int size;
    private ArrayList<T> vector;
    private static final int INCREMENT_FACTOR = 5;
 
    public Vector(int size)
    {
        this.size = size;
        this.capacity = size + INCREMENT_FACTOR;
        vector = new ArrayList<T>();
    }
 
    public void store(int index, T value)
    {
        try 
        {
            vector.set(index, value);
        } catch (IndexOutOfBoundsException indexOutBounds)
        {
            if (index >= 0 && (index < size))
            {
                vector.add(index, value);
            }
            if (index >= 0 && (index >= size && index < capacity))
            {
                vector.add(index, value);
                size = index + 1;
                if (size == capacity)
                    capacity = capacity + INCREMENT_FACTOR;
            }
            if (index >= capacity)
            {
                throw new IndexOutOfBoundsException();
            }
        }
    }
 
    public T get(int index)
    {
        try
        {
            return vector.get(index);
        } catch (IndexOutOfBoundsException indexOutBounds)
        {
        }
        return null;
    }
 
    public int getSize()
    {
        return size;
    }
 
    public int getCapacity()
    {
        return capacity;
    }
 
    public static void main(String... arg)
    {
        int size;
        int num;
        int value;
 
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the initial size of the vector");
        size = scanner.nextInt();
 
        Vector<Integer> vector = new Vector<>(size);
        System.out.println("Enter the number of elements ");
        num = scanner.nextInt();
 
        System.out.println("Enter the values");
        for (int index = 0; index < num; index++)
        {
            value = scanner.nextInt();
            vector.store(index, value);
        }
 
        System.out.println("The Entered Values are");
        for (int index = 0; index < vector.getSize(); index++)
        {
            System.out.print(vector.get(index) + "\t");
        }
 
        System.out.println("\nTHE SIZE OF THE VECTOR IS  " + vector.getSize());
        System.out.println("THE CAPACITY OF THE VECTOR IS  " + vector.getCapacity());
        scanner.close();
    }
}
$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

Related posts:

Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Spring – Injecting Collections
Java Program to Implement Stack using Two Queues
Intro to the Jackson ObjectMapper
OAuth2 Remember Me with Refresh Token
Java Program to Check Whether a Given Point is in a Given Polygon
Explain about URL and HTTPS protocol
Java Program to Permute All Letters of an Input String
Java Program to Implement Find all Back Edges in a Graph
So sánh HashMap và HashSet trong Java
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Implement Segment Tree
Spring Boot Application as a Service
Java Program to Implement Fermat Primality Test Algorithm
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Map to String Conversion in Java
Từ khóa static và final trong java
Compare Two JSON Objects with Jackson
How to Kill a Java Thread
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Implement Counting Sort
Dockerizing a Spring Boot Application
Java 8 – Powerful Comparison with Lambdas
Các nguyên lý thiết kế hướng đối tượng – SOLID
Java Program to Implement HashMap API
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Using Spring @ResponseStatus to Set HTTP Status Code
Java Map With Case-Insensitive Keys
Java Program to Implement Adjacency List
A Guide to LinkedHashMap in Java
Java Program to Implement SimpeBindings API