This Java program is to Implement Variable length array. In programming, a variable-length array (or VLA) is an array data structure of automatic storage duration whose length is determined at run time (instead of at compile time).
Here is the source code of the Java program to implement variable length array. 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 VariableLengthArray<T>
{
private volatile int size;
private ArrayList<T> array;
public VariableLengthArray()
{
array = new ArrayList<T>();
setSize(-1);
}
public void setSize(int size)
{
this.size = size;
}
public int getSize()
{
return size;
}
public void store(int index, T value)
{
try
{
array.set(index, value);
} catch (IndexOutOfBoundsException indexOutBounds)
{
if (index >= 0 && !(index < size))
{
throw new IndexOutOfBoundsException();
}
array.add(index, value);
}
}
public T get(int index)
{
try
{
if (index >= 0 && index < size)
return array.get(index);
else
throw new IndexOutOfBoundsException();
} catch (IndexOutOfBoundsException indexOutBounds)
{
System.out.println("INDEX OUT OF BOUNDS : the specified index is
more than the size of the array");
}
return null;
}
public T remove(int index)
{
try
{
if (index >= 0 && index < size)
{
size--;
return array.remove(index);
} else
throw new IndexOutOfBoundsException();
} catch (IndexOutOfBoundsException indexOutBounds)
{
System.out.println("INDEX OUT OF BOUNDS : the specified index
is more than the size of the array");
}
return null;
}
public static void main(String... arg)
{
int size, value;
String choice;
Scanner scanner = new Scanner(System.in);
VariableLengthArray<Integer> integersArray = new VariableLengthArray<Integer>();
do
{
System.out.println("Enter the size of the array");
size = scanner.nextInt();
integersArray.setSize(size);
System.out.println("Enter the values of the array");
for (int index = 0; index < integersArray.getSize(); index++)
{
value = scanner.nextInt();
integersArray.store(index, value);
}
System.out.println("The Values entered are ");
for (int index = 0; index < integersArray.getSize(); index++)
{
System.out.print(integersArray.get(index) + "\t");
}
System.out.println("\nEnter more values ?[y/n]");
choice = scanner.next();
} while (choice.equals("y"));
scanner.close();
}
}
$javac VariableLengthArray.java $java VariableLengthArray Enter the size of the array 5 Enter the values of the array 10 9 8 7 6 The Values entered are 10 9 8 7 6 Enter more values ?[y/n] y Enter the size of the array 3 Enter the values of the array 2 3 4 The Values entered are 2 3 4
Related posts:
Lập trình đa luồng với Callable và Future trong Java
Spring Boot: Customize the Jackson ObjectMapper
What is a POJO Class?
A Guide to the Java LinkedList
Tính trừu tượng (Abstraction) trong Java
Hướng dẫn sử dụng Lớp FilePermission trong java
Java Program to Generate Random Hexadecimal Byte
StringBuilder vs StringBuffer in Java
Deploy a Spring Boot WAR into a Tomcat Server
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Java Program to Implement SimpeBindings API
Java Program to Implement Max Heap
The Registration API becomes RESTful
JUnit 5 for Kotlin Developers
Java Program to Implement ConcurrentLinkedQueue API
The Java 8 Stream API Tutorial
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Deep Learning Essentials - Yusuke Sugomori
Sử dụng CyclicBarrier trong Java
Jackson – Marshall String to JsonNode
Java Program to Implement Insertion Sort
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
How to Define a Spring Boot Filter?
Java Program to Implement Threaded Binary Tree
Spring @Primary Annotation
Sorting in Java
Giới thiệu Aspect Oriented Programming (AOP)
Using JWT with Spring Security OAuth
Phương thức tham chiếu trong Java 8 – Method References
Simultaneous Spring WebClient Calls
Introduction to the Functional Web Framework in Spring 5
Java Program to Check Cycle in a Graph using Topological Sort