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:
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
A Guide to ConcurrentMap
Annotation trong Java 8
Guide to Selenium with JUnit / TestNG
Spring Security Basic Authentication
Java Program to Implement Horner Algorithm
Servlet 3 Async Support with Spring MVC and Spring Security
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Converting String to Stream of chars
Spring Boot - Apache Kafka
A Guide to Spring Boot Admin
Guide to Spring 5 WebFlux
Java Timer
Rate Limiting in Spring Cloud Netflix Zuul
How to Read a File in Java
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Integer Constant Pool trong Java
Introduction to Spring Method Security
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Implement IdentityHashMap API
Java 14 Record Keyword
Java Program to Implement Vector API
Java IO vs NIO
Java Program to Implement Knapsack Algorithm
Lập trình đa luồng trong Java (Java Multi-threading)
Java Program to add two large numbers using Linked List
Java Program to Find the Longest Path in a DAG
Getting Started with GraphQL and Spring Boot
Java Program to Implement Find all Forward Edges in a Graph
Daemon Threads in Java
Use Liquibase to Safely Evolve Your Database Schema
Java Program for Douglas-Peucker Algorithm Implementation