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 Implement Stein GCD Algorithm
Extract links from an HTML page
Spring Boot - Zuul Proxy Server and Routing
Toán tử instanceof trong java
Apache Commons Collections OrderedMap
Java Program to Perform Polygon Containment Test
HashSet trong java
Hướng dẫn Java Design Pattern – State
An Intro to Spring Cloud Task
Spring Boot - Introduction
Java Program to Perform Insertion in a BST
Spring 5 and Servlet 4 – The PushBuilder
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Guide to Java OutputStream
Guide to the Java TransferQueue
Spring Boot - Tracing Micro Service Logs
Java Program to Implement Euclid GCD Algorithm
Java Program to Represent Graph Using Linked List
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Setting a Request Timeout for a Spring REST API
Using the Map.Entry Java Class
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement LinkedList API
So sánh ArrayList và Vector trong Java
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement Floyd Cycle Algorithm
Jackson – Marshall String to JsonNode
Validations for Enum Types
Spring Boot - Logging
Documenting a Spring REST API Using OpenAPI 3.0
Constructor Dependency Injection in Spring