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 the GCD and LCM of two Numbers
Spring Boot - Web Socket
Guide to UUID in Java
Spring Cloud – Bootstrapping
An Example of Load Balancing with Zuul and Eureka
Java – Reader to String
Spring NoSuchBeanDefinitionException
Converting Between an Array and a Set in Java
Request Method Not Supported (405) in Spring
Java Program to Check whether Undirected Graph is Connected using DFS
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Guide to the Fork/Join Framework in Java
Logout in an OAuth Secured Application
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program to Implement Doubly Linked List
Lớp Collections trong Java (Collections Utility Class)
A Guide to Spring Boot Admin
Hướng dẫn sử dụng Java Reflection
A Guide to the finalize Method in Java
Guide to the Java Clock Class
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Perform integer Partition for a Specific Case
Từ khóa static và final trong java
REST Web service: Upload và Download file với Jersey 2.x
Overview of the java.util.concurrent
Generic Constructors in Java
Java Program to Solve Knapsack Problem Using Dynamic Programming
Spring Data JPA Delete and Relationships
JUnit 5 for Kotlin Developers
Tạo số và chuỗi ngẫu nhiên trong Java
Java NIO2 Path API
Java Scanner hasNext() vs. hasNextLine()