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:
String Joiner trong Java 8
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Unsatisfied Dependency in Spring
Java Program to Implement String Matching Using Vectors
Apache Camel with Spring Boot
Base64 encoding và decoding trong Java 8
Java Program to Implement Binomial Heap
Add Multiple Items to an Java ArrayList
Jackson – JsonMappingException (No serializer found for class)
Get and Post Lists of Objects with RestTemplate
Java Program to Solve Knapsack Problem Using Dynamic Programming
Spring @Primary Annotation
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Using the Map.Entry Java Class
A Guide to the finalize Method in Java
List Interface trong Java
Java Program to Implement Segment Tree
Java Program to Implement Nth Root Algorithm
Spring Cloud AWS – Messaging Support
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Sorting Query Results with Spring Data
HashSet trong Java hoạt động như thế nào?
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Perform Partition of an Integer in All Possible Ways
Number Formatting in Java
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Spring Data JPA @Modifying Annotation
Understanding Memory Leaks in Java
Collect a Java Stream to an Immutable Collection
Lớp Collections trong Java (Collections Utility Class)
How to Store Duplicate Keys in a Map in Java?
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling