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:
Hướng dẫn Java Design Pattern – DAO
A Guide To UDP In Java
Java Program to Solve Knapsack Problem Using Dynamic Programming
Hướng dẫn Java Design Pattern – Adapter
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Implement Quick Sort Using Randomization
Java Program to Implement LinkedList API
Guide to the Java TransferQueue
Java Program to Represent Graph Using Incidence Matrix
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Java Program to Represent Graph Using Linked List
Spring Data MongoDB – Indexes, Annotations and Converters
How to Kill a Java Thread
How to Set TLS Version in Apache HttpClient
Mockito and JUnit 5 – Using ExtendWith
Hướng dẫn Java Design Pattern – Flyweight
Java Program to Perform Uniform Binary Search
Custom JUnit 4 Test Runners
Java Program to Implement Quick Sort with Given Complexity Constraint
Control Structures in Java
Send email with JavaMail
Java Program to Find Maximum Element in an Array using Binary Search
Redirect to Different Pages after Login with Spring Security
Java Program to Implement Knapsack Algorithm
Hướng dẫn Java Design Pattern – Null Object
How to Delay Code Execution in Java
Java Program to Implement Coppersmith Freivald’s Algorithm
How To Serialize and Deserialize Enums with Jackson
Convert XML to JSON Using Jackson
Apache Commons Collections Bag
Java – Write an InputStream to a File
Display Auto-Configuration Report in Spring Boot